mt_gpio.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /******************************************************************************
  2. * mt_gpio.c - MTKLinux GPIO Device Driver
  3. *
  4. * Copyright 2008-2009 MediaTek Co.,Ltd.
  5. *
  6. * DESCRIPTION:
  7. * This file provid the other drivers GPIO relative functions
  8. *
  9. ******************************************************************************/
  10. #include <platform/mt_reg_base.h>
  11. #include <platform/mt_gpio.h>
  12. #include <platform/gpio_cfg.h>
  13. #include <debug.h>
  14. /******************************************************************************
  15. MACRO Definition
  16. ******************************************************************************/
  17. //#define GIO_SLFTEST
  18. #define GPIO_DEVICE "mt-gpio"
  19. #define VERSION GPIO_DEVICE
  20. /*---------------------------------------------------------------------------*/
  21. #define GPIO_WR32(addr, data) DRV_WriteReg32(addr,data)
  22. #define GPIO_RD32(addr) DRV_Reg32(addr)
  23. #define GPIO_SW_SET_BITS(BIT,REG) GPIO_WR32(REG,GPIO_RD32(REG) | ((unsigned long)(BIT)))
  24. #define GPIO_SET_BITS(BIT,REG) ((*(volatile u32*)(REG)) = (u32)(BIT))
  25. #define GPIO_CLR_BITS(BIT,REG) ((*(volatile u32*)(REG)) &= ~((u32)(BIT)))
  26. /*---------------------------------------------------------------------------*/
  27. #define TRUE 1
  28. #define FALSE 0
  29. /*---------------------------------------------------------------------------*/
  30. //#define MAX_GPIO_REG_BITS 16
  31. //#define MAX_GPIO_MODE_PER_REG 5
  32. //#define GPIO_MODE_BITS 3
  33. /*---------------------------------------------------------------------------*/
  34. #define GPIOTAG "[GPIO] "
  35. #define GPIOLOG(fmt, arg...) dprintf(INFO,GPIOTAG fmt, ##arg)
  36. #define GPIOMSG(fmt, arg...) dprintf(INFO,fmt, ##arg)
  37. #define GPIOERR(fmt, arg...) dprintf(INFO,GPIOTAG "%5d: "fmt, __LINE__, ##arg)
  38. #define GPIOFUC(fmt, arg...) //dprintk(INFO,GPIOTAG "%s\n", __FUNCTION__)
  39. #define GIO_INVALID_OBJ(ptr) ((ptr) != gpio_obj)
  40. /******************************************************************************
  41. Enumeration/Structure
  42. ******************************************************************************/
  43. #if defined(MACH_FPGA)
  44. S32 mt_set_gpio_dir(u32 pin, u32 dir) {return RSUCCESS;}
  45. S32 mt_get_gpio_dir(u32 pin) {return GPIO_DIR_UNSUPPORTED;}
  46. S32 mt_set_gpio_pull_enable(u32 pin, u32 enable) {return RSUCCESS;}
  47. S32 mt_get_gpio_pull_enable(u32 pin) {return GPIO_PULL_EN_UNSUPPORTED;}
  48. S32 mt_set_gpio_pull_select(u32 pin, u32 select) {return RSUCCESS;}
  49. S32 mt_get_gpio_pull_select(u32 pin) {return GPIO_PULL_UNSUPPORTED;}
  50. S32 mt_set_gpio_smt(u32 pin, u32 enable) {return RSUCCESS;}
  51. S32 mt_get_gpio_smt(u32 pin) {return GPIO_SMT_UNSUPPORTED;}
  52. S32 mt_set_gpio_ies(u32 pin, u32 enable) {return RSUCCESS;}
  53. S32 mt_get_gpio_ies(u32 pin) {return GPIO_IES_UNSUPPORTED;}
  54. S32 mt_set_gpio_out(u32 pin, u32 output) {return RSUCCESS;}
  55. S32 mt_get_gpio_out(u32 pin) {return GPIO_OUT_UNSUPPORTED;}
  56. S32 mt_get_gpio_in(u32 pin) {return GPIO_IN_UNSUPPORTED;}
  57. S32 mt_set_gpio_mode(u32 pin, u32 mode) {return RSUCCESS;}
  58. S32 mt_get_gpio_mode(u32 pin) {return GPIO_MODE_UNSUPPORTED;}
  59. #else
  60. struct mt_gpio_obj {
  61. GPIO_REGS *reg;
  62. };
  63. static struct mt_gpio_obj gpio_dat = {
  64. .reg = (GPIO_REGS*)(GPIO_BASE),
  65. };
  66. static struct mt_gpio_obj *gpio_obj = &gpio_dat;
  67. /*---------------------------------------------------------------------------*/
  68. S32 mt_set_gpio_dir_chip(u32 pin, u32 dir)
  69. {
  70. u32 pos;
  71. u32 bit;
  72. struct mt_gpio_obj *obj = gpio_obj;
  73. if (!obj)
  74. return -ERACCESS;
  75. if (pin >= MAX_GPIO_PIN)
  76. return -ERINVAL;
  77. if (dir >= GPIO_DIR_MAX)
  78. return -ERINVAL;
  79. pos = pin / MAX_GPIO_REG_BITS;
  80. bit = pin % MAX_GPIO_REG_BITS;
  81. if (dir == GPIO_DIR_IN)
  82. GPIO_SET_BITS((1L << bit), &obj->reg->dir[pos].rst);
  83. else
  84. GPIO_SET_BITS((1L << bit), &obj->reg->dir[pos].set);
  85. return RSUCCESS;
  86. }
  87. /*---------------------------------------------------------------------------*/
  88. S32 mt_get_gpio_dir_chip(u32 pin)
  89. {
  90. u32 pos;
  91. u32 bit;
  92. u32 reg;
  93. struct mt_gpio_obj *obj = gpio_obj;
  94. if (!obj)
  95. return -ERACCESS;
  96. if (pin >= MAX_GPIO_PIN)
  97. return -ERINVAL;
  98. pos = pin / MAX_GPIO_REG_BITS;
  99. bit = pin % MAX_GPIO_REG_BITS;
  100. reg = GPIO_RD32(&obj->reg->dir[pos].val);
  101. return (((reg & (1L << bit)) != 0)? 1: 0);
  102. }
  103. /*---------------------------------------------------------------------------*/
  104. S32 mt_set_gpio_pull_enable_chip(u32 pin, u32 enable)
  105. {
  106. if (pin >= MAX_GPIO_PIN)
  107. return -ERINVAL;
  108. if(PULLEN_offset[pin].offset == -1){
  109. return GPIO_PULL_EN_UNSUPPORTED;
  110. }
  111. else{
  112. if (enable == GPIO_PULL_DISABLE)
  113. GPIO_SET_BITS((1L << (PULLEN_offset[pin].offset)), PULLEN_addr[pin].addr + 8);
  114. else
  115. GPIO_SET_BITS((1L << (PULLEN_offset[pin].offset)), PULLEN_addr[pin].addr + 4);
  116. }
  117. return RSUCCESS;
  118. }
  119. /*---------------------------------------------------------------------------*/
  120. S32 mt_get_gpio_pull_enable_chip(u32 pin)
  121. {
  122. unsigned long data;
  123. if (pin >= MAX_GPIO_PIN)
  124. return -ERINVAL;
  125. if(PULLEN_offset[pin].offset == -1){
  126. return GPIO_PULL_EN_UNSUPPORTED;
  127. }
  128. else{
  129. data = GPIO_RD32(PULLEN_addr[pin].addr);
  130. return (((data & (1L << (PULLEN_offset[pin].offset))) != 0)? 1: 0);
  131. }
  132. }
  133. /*---------------------------------------------------------------------------*/
  134. S32 mt_set_gpio_smt_chip(u32 pin, u32 enable)
  135. {
  136. //unsigned long flags;
  137. if (pin >= MAX_GPIO_PIN)
  138. return -ERINVAL;
  139. if(SMT_offset[pin].offset == -1){
  140. return GPIO_SMT_UNSUPPORTED;
  141. }
  142. else{
  143. if (enable == GPIO_SMT_DISABLE)
  144. GPIO_SET_BITS((1L << (SMT_offset[pin].offset)), SMT_addr[pin].addr + 8);
  145. else
  146. GPIO_SET_BITS((1L << (SMT_offset[pin].offset)), SMT_addr[pin].addr + 4);
  147. }
  148. return RSUCCESS;
  149. }
  150. /*---------------------------------------------------------------------------*/
  151. S32 mt_get_gpio_smt_chip(u32 pin)
  152. {
  153. unsigned long data;
  154. if (pin >= MAX_GPIO_PIN)
  155. return -ERINVAL;
  156. if(SMT_offset[pin].offset == -1){
  157. return GPIO_SMT_UNSUPPORTED;
  158. }
  159. else{
  160. data = GPIO_RD32(SMT_addr[pin].addr);
  161. return (((data & (1L << (SMT_offset[pin].offset))) != 0)? 1: 0);
  162. }
  163. }
  164. /*---------------------------------------------------------------------------*/
  165. S32 mt_set_gpio_ies_chip(u32 pin, u32 enable)
  166. {
  167. //unsigned long flags;
  168. if (pin >= MAX_GPIO_PIN)
  169. return -ERINVAL;
  170. if(IES_offset[pin].offset == -1){
  171. return GPIO_IES_UNSUPPORTED;
  172. }
  173. else{
  174. if (enable == GPIO_IES_DISABLE)
  175. GPIO_SET_BITS((1L << (IES_offset[pin].offset)), IES_addr[pin].addr + 8);
  176. else
  177. GPIO_SET_BITS((1L << (IES_offset[pin].offset)), IES_addr[pin].addr + 4);
  178. }
  179. return RSUCCESS;
  180. }
  181. /*---------------------------------------------------------------------------*/
  182. S32 mt_get_gpio_ies_chip(u32 pin)
  183. {
  184. unsigned long data;
  185. if (pin >= MAX_GPIO_PIN)
  186. return -ERINVAL;
  187. if(IES_offset[pin].offset == -1){
  188. return GPIO_IES_UNSUPPORTED;
  189. }
  190. else{
  191. data = GPIO_RD32(IES_addr[pin].addr);
  192. return (((data & (1L << (IES_offset[pin].offset))) != 0)? 1: 0);
  193. }
  194. }
  195. /*---------------------------------------------------------------------------*/
  196. S32 mt_set_gpio_pull_select_chip(u32 pin, u32 select)
  197. {
  198. //unsigned long flags;
  199. if (pin >= MAX_GPIO_PIN)
  200. return -ERINVAL;
  201. if((PULL_offset[pin].offset == -1) && (PU_offset[pin].offset == -1)){
  202. return GPIO_PULL_UNSUPPORTED;
  203. }
  204. else if(PULL_offset[pin].offset == -1){
  205. mutex_acquire(&gpio_mutex);
  206. if (select == GPIO_PULL_DOWN){
  207. GPIO_CLR_BITS((1L << (PU_offset[pin].offset)), PU_addr[pin].addr);
  208. GPIO_SW_SET_BITS((1L << (PD_offset[pin].offset)), PD_addr[pin].addr);
  209. }
  210. else {
  211. GPIO_CLR_BITS((1L << (PD_offset[pin].offset)), PD_addr[pin].addr);
  212. GPIO_SW_SET_BITS((1L << (PU_offset[pin].offset)), PU_addr[pin].addr);
  213. }
  214. mutex_release(&gpio_mutex);
  215. }
  216. else{
  217. if (((pin >= 41) && (pin < 58)) || (pin == 64) || (pin == 65) || (pin == 67) || (pin == 69)){
  218. if (select == GPIO_PULL_DOWN)
  219. GPIO_SET_BITS((1L << (PULL_offset[pin].offset)), PULL_addr[pin].addr + 4);
  220. else
  221. GPIO_SET_BITS((1L << (PULL_offset[pin].offset)), PULL_addr[pin].addr + 8);
  222. }
  223. else {
  224. if (select == GPIO_PULL_DOWN)
  225. GPIO_SET_BITS((1L << (PULL_offset[pin].offset)), PULL_addr[pin].addr + 8);
  226. else
  227. GPIO_SET_BITS((1L << (PULL_offset[pin].offset)), PULL_addr[pin].addr + 4);
  228. }
  229. }
  230. return RSUCCESS;
  231. }
  232. /*---------------------------------------------------------------------------*/
  233. S32 mt_get_gpio_pull_select_chip(u32 pin)
  234. {
  235. unsigned long data;
  236. if (pin >= MAX_GPIO_PIN)
  237. return -ERINVAL;
  238. if((PULL_offset[pin].offset == -1) && (PU_offset[pin].offset == -1)){
  239. return GPIO_PULL_UNSUPPORTED;
  240. }
  241. else if(PULL_offset[pin].offset == -1){
  242. data = GPIO_RD32(PU_addr[pin].addr);
  243. return (((data & (1L << (PU_offset[pin].offset))) != 0)? 1: 0);
  244. }
  245. else{
  246. if (((pin >= 41) && (pin < 58)) || (pin == 64) || (pin == 65) || (pin == 67) || (pin == 69)){
  247. data = GPIO_RD32(PULL_addr[pin].addr);
  248. return (((data & (1L << (PULL_offset[pin].offset))) != 0)? 0: 1);
  249. }
  250. else {
  251. data = GPIO_RD32(PULL_addr[pin].addr);
  252. return (((data & (1L << (PULL_offset[pin].offset))) != 0)? 1: 0);
  253. }
  254. }
  255. }
  256. /*---------------------------------------------------------------------------*/
  257. S32 mt_set_gpio_out_chip(u32 pin, u32 output)
  258. {
  259. u32 pos;
  260. u32 bit;
  261. struct mt_gpio_obj *obj = gpio_obj;
  262. if (!obj)
  263. return -ERACCESS;
  264. if (pin >= MAX_GPIO_PIN)
  265. return -ERINVAL;
  266. if (output >= GPIO_OUT_MAX)
  267. return -ERINVAL;
  268. pos = pin / MAX_GPIO_REG_BITS;
  269. bit = pin % MAX_GPIO_REG_BITS;
  270. if (output == GPIO_OUT_ZERO)
  271. GPIO_SET_BITS((1L << bit), &obj->reg->dout[pos].rst);
  272. else
  273. GPIO_SET_BITS((1L << bit), &obj->reg->dout[pos].set);
  274. return RSUCCESS;
  275. }
  276. /*---------------------------------------------------------------------------*/
  277. S32 mt_get_gpio_out_chip(u32 pin)
  278. {
  279. u32 pos;
  280. u32 bit;
  281. u32 reg;
  282. struct mt_gpio_obj *obj = gpio_obj;
  283. if (!obj)
  284. return -ERACCESS;
  285. if (pin >= MAX_GPIO_PIN)
  286. return -ERINVAL;
  287. pos = pin / MAX_GPIO_REG_BITS;
  288. bit = pin % MAX_GPIO_REG_BITS;
  289. reg = GPIO_RD32(&obj->reg->dout[pos].val);
  290. return (((reg & (1L << bit)) != 0)? 1: 0);
  291. }
  292. /*---------------------------------------------------------------------------*/
  293. S32 mt_get_gpio_in_chip(u32 pin)
  294. {
  295. u32 pos;
  296. u32 bit;
  297. u32 reg;
  298. struct mt_gpio_obj *obj = gpio_obj;
  299. if (!obj)
  300. return -ERACCESS;
  301. if (pin >= MAX_GPIO_PIN)
  302. return -ERINVAL;
  303. pos = pin / MAX_GPIO_REG_BITS;
  304. bit = pin % MAX_GPIO_REG_BITS;
  305. reg = GPIO_RD32(&obj->reg->din[pos].val);
  306. return (((reg & (1L << bit)) != 0)? 1: 0);
  307. }
  308. /*---------------------------------------------------------------------------*/
  309. S32 mt_set_gpio_mode_chip(u32 pin, u32 mode)
  310. {
  311. u32 pos;
  312. u32 bit;
  313. u32 reg;
  314. //u32 mask = (1L << GPIO_MODE_BITS) - 1;
  315. struct mt_gpio_obj *obj = gpio_obj;
  316. if (!obj)
  317. return -ERACCESS;
  318. if (pin >= MAX_GPIO_PIN)
  319. return -ERINVAL;
  320. if (mode >= GPIO_MODE_MAX)
  321. return -ERINVAL;
  322. pos = pin / MAX_GPIO_MODE_PER_REG;
  323. bit = pin % MAX_GPIO_MODE_PER_REG;
  324. #if 0
  325. reg = GPIO_RD32(&obj->reg->mode[pos].val);
  326. reg &= ~(mask << (GPIO_MODE_BITS*bit));
  327. reg |= (mode << (GPIO_MODE_BITS*bit));
  328. GPIO_WR32(&obj->reg->mode[pos].val, reg);
  329. #endif
  330. reg = ((1L << (GPIO_MODE_BITS*bit)) << 3) | (mode << (GPIO_MODE_BITS*bit));
  331. GPIO_WR32(&obj->reg->mode[pos]._align1, reg);
  332. return RSUCCESS;
  333. }
  334. /*---------------------------------------------------------------------------*/
  335. S32 mt_get_gpio_mode_chip(u32 pin)
  336. {
  337. u32 pos;
  338. u32 bit;
  339. u32 reg;
  340. u32 mask = (1L << GPIO_MODE_BITS) - 1;
  341. struct mt_gpio_obj *obj = gpio_obj;
  342. if (!obj)
  343. return -ERACCESS;
  344. if (pin >= MAX_GPIO_PIN)
  345. return -ERINVAL;
  346. pos = pin / MAX_GPIO_MODE_PER_REG;
  347. bit = pin % MAX_GPIO_MODE_PER_REG;
  348. reg = GPIO_RD32(&obj->reg->mode[pos].val);
  349. return ((reg >> (GPIO_MODE_BITS*bit)) & mask);
  350. }
  351. /*---------------------------------------------------------------------------*/
  352. void mt_gpio_pin_decrypt(u32 *cipher)
  353. {
  354. //just for debug, find out who used pin number directly
  355. if((*cipher & (0x80000000)) == 0){
  356. GPIOERR("GPIO%u HARDCODE warning!!! \n",(unsigned int)*cipher);
  357. //dump_stack();
  358. //return;
  359. }
  360. //GPIOERR("Pin magic number is %x\n",*cipher);
  361. *cipher &= ~(0x80000000);
  362. return;
  363. }
  364. //set GPIO function in fact
  365. /*---------------------------------------------------------------------------*/
  366. S32 mt_set_gpio_dir(u32 pin, u32 dir)
  367. {
  368. mt_gpio_pin_decrypt(&pin);
  369. return mt_set_gpio_dir_chip(pin,dir);
  370. }
  371. /*---------------------------------------------------------------------------*/
  372. S32 mt_get_gpio_dir(u32 pin)
  373. {
  374. mt_gpio_pin_decrypt(&pin);
  375. return mt_get_gpio_dir_chip(pin);
  376. }
  377. /*---------------------------------------------------------------------------*/
  378. S32 mt_set_gpio_pull_enable(u32 pin, u32 enable)
  379. {
  380. mt_gpio_pin_decrypt(&pin);
  381. return mt_set_gpio_pull_enable_chip(pin,enable);
  382. }
  383. /*---------------------------------------------------------------------------*/
  384. S32 mt_get_gpio_pull_enable(u32 pin)
  385. {
  386. mt_gpio_pin_decrypt(&pin);
  387. return mt_get_gpio_pull_enable_chip(pin);
  388. }
  389. /*---------------------------------------------------------------------------*/
  390. S32 mt_set_gpio_pull_select(u32 pin, u32 select)
  391. {
  392. mt_gpio_pin_decrypt(&pin);
  393. return mt_set_gpio_pull_select_chip(pin,select);
  394. }
  395. /*---------------------------------------------------------------------------*/
  396. S32 mt_get_gpio_pull_select(u32 pin)
  397. {
  398. mt_gpio_pin_decrypt(&pin);
  399. return mt_get_gpio_pull_select_chip(pin);
  400. }
  401. /*---------------------------------------------------------------------------*/
  402. S32 mt_set_gpio_smt(u32 pin, u32 enable)
  403. {
  404. mt_gpio_pin_decrypt(&pin);
  405. return mt_set_gpio_smt_chip(pin,enable);
  406. }
  407. /*---------------------------------------------------------------------------*/
  408. S32 mt_get_gpio_smt(u32 pin)
  409. {
  410. mt_gpio_pin_decrypt(&pin);
  411. return mt_get_gpio_smt_chip(pin);
  412. }
  413. /*---------------------------------------------------------------------------*/
  414. S32 mt_set_gpio_ies(u32 pin, u32 enable)
  415. {
  416. mt_gpio_pin_decrypt(&pin);
  417. return mt_set_gpio_ies_chip(pin,enable);
  418. }
  419. /*---------------------------------------------------------------------------*/
  420. S32 mt_get_gpio_ies(u32 pin)
  421. {
  422. mt_gpio_pin_decrypt(&pin);
  423. return mt_get_gpio_ies_chip(pin);
  424. }
  425. /*---------------------------------------------------------------------------*/
  426. S32 mt_set_gpio_out(u32 pin, u32 output)
  427. {
  428. mt_gpio_pin_decrypt(&pin);
  429. return mt_set_gpio_out_chip(pin,output);
  430. }
  431. /*---------------------------------------------------------------------------*/
  432. S32 mt_get_gpio_out(u32 pin)
  433. {
  434. mt_gpio_pin_decrypt(&pin);
  435. return mt_get_gpio_out_chip(pin);
  436. }
  437. /*---------------------------------------------------------------------------*/
  438. S32 mt_get_gpio_in(u32 pin)
  439. {
  440. mt_gpio_pin_decrypt(&pin);
  441. return mt_get_gpio_in_chip(pin);
  442. }
  443. /*---------------------------------------------------------------------------*/
  444. S32 mt_set_gpio_mode(u32 pin, u32 mode)
  445. {
  446. mt_gpio_pin_decrypt(&pin);
  447. return mt_set_gpio_mode_chip(pin,mode);
  448. }
  449. /*---------------------------------------------------------------------------*/
  450. S32 mt_get_gpio_mode(u32 pin)
  451. {
  452. mt_gpio_pin_decrypt(&pin);
  453. return mt_get_gpio_mode_chip(pin);
  454. }
  455. /*****************************************************************************/
  456. /* sysfs operation */
  457. /*****************************************************************************/
  458. #if 0
  459. void mt_gpio_self_test(void)
  460. {
  461. int i, val;
  462. for (i = 0; i < GPIO_MAX; i++)
  463. {
  464. S32 res,old;
  465. GPIOMSG("GPIO-%3d test\n", i);
  466. /*direction test*/
  467. old = mt_get_gpio_dir(i);
  468. if (old == 0 || old == 1) {
  469. GPIOLOG(" dir old = %d\n", old);
  470. } else {
  471. GPIOERR(" test dir fail: %d\n", old);
  472. break;
  473. }
  474. if ((res = mt_set_gpio_dir(i, GPIO_DIR_OUT)) != RSUCCESS) {
  475. GPIOERR(" set dir out fail: %d\n", res);
  476. break;
  477. } else if ((res = mt_get_gpio_dir(i)) != GPIO_DIR_OUT) {
  478. GPIOERR(" get dir out fail: %d\n", res);
  479. break;
  480. } else {
  481. /*output test*/
  482. S32 out = mt_get_gpio_out(i);
  483. if (out != 0 && out != 1) {
  484. GPIOERR(" get out fail = %d\n", old);
  485. break;
  486. }
  487. for (val = 0; val < GPIO_OUT_MAX; val++) {
  488. if ((res = mt_set_gpio_out(i,0)) != RSUCCESS) {
  489. GPIOERR(" set out[%d] fail: %d\n", val, res);
  490. break;
  491. } else if ((res = mt_get_gpio_out(i)) != 0) {
  492. GPIOERR(" get out[%d] fail: %d\n", val, res);
  493. break;
  494. }
  495. }
  496. if ((res = mt_set_gpio_out(i,out)) != RSUCCESS)
  497. {
  498. GPIOERR(" restore out fail: %d\n", res);
  499. break;
  500. }
  501. }
  502. if ((res = mt_set_gpio_dir(i, GPIO_DIR_IN)) != RSUCCESS) {
  503. GPIOERR(" set dir in fail: %d\n", res);
  504. break;
  505. } else if ((res = mt_get_gpio_dir(i)) != GPIO_DIR_IN) {
  506. GPIOERR(" get dir in fail: %d\n", res);
  507. break;
  508. } else {
  509. GPIOLOG(" input data = %d\n", res);
  510. }
  511. if ((res = mt_set_gpio_dir(i, old)) != RSUCCESS) {
  512. GPIOERR(" restore dir fail: %d\n", res);
  513. break;
  514. }
  515. for (val = 0; val < GPIO_PULL_EN_MAX; val++) {
  516. if ((res = mt_set_gpio_pull_enable(i,val)) != RSUCCESS) {
  517. GPIOERR(" set pullen[%d] fail: %d\n", val, res);
  518. break;
  519. } else if ((res = mt_get_gpio_pull_enable(i)) != val) {
  520. GPIOERR(" get pullen[%d] fail: %d\n", val, res);
  521. break;
  522. }
  523. }
  524. if ((res = mt_set_gpio_pull_enable(i, old)) != RSUCCESS) {
  525. GPIOERR(" restore pullen fail: %d\n", res);
  526. break;
  527. }
  528. /*pull select test*/
  529. old = mt_get_gpio_pull_select(i);
  530. if (old == 0 || old == 1)
  531. GPIOLOG(" pullsel old = %d\n", old);
  532. else {
  533. GPIOERR(" pullsel fail: %d\n", old);
  534. break;
  535. }
  536. for (val = 0; val < GPIO_PULL_MAX; val++) {
  537. if ((res = mt_set_gpio_pull_select(i,val)) != RSUCCESS) {
  538. GPIOERR(" set pullsel[%d] fail: %d\n", val, res);
  539. break;
  540. } else if ((res = mt_get_gpio_pull_select(i)) != val) {
  541. GPIOERR(" get pullsel[%d] fail: %d\n", val, res);
  542. break;
  543. }
  544. }
  545. if ((res = mt_set_gpio_pull_select(i, old)) != RSUCCESS)
  546. {
  547. GPIOERR(" restore pullsel fail: %d\n", res);
  548. break;
  549. }
  550. /*mode control*/
  551. old = mt_get_gpio_mode(i);
  552. if ((old >= GPIO_MODE_00) && (val < GPIO_MODE_MAX)){
  553. GPIOLOG(" mode old = %d\n", old);
  554. }
  555. else{
  556. GPIOERR(" get mode fail: %d\n", old);
  557. break;
  558. }
  559. for (val = 0; val < GPIO_MODE_MAX; val++) {
  560. if ((res = mt_set_gpio_mode(i, val)) != RSUCCESS) {
  561. GPIOERR("set mode[%d] fail: %d\n", val, res);
  562. break;
  563. } else if ((res = mt_get_gpio_mode(i)) != val) {
  564. GPIOERR("get mode[%d] fail: %d\n", val, res);
  565. break;
  566. }
  567. }
  568. if ((res = mt_set_gpio_mode(i,old)) != RSUCCESS) {
  569. GPIOERR(" restore mode fail: %d\n", res);
  570. break;
  571. }
  572. }
  573. GPIOLOG("GPIO test done\n");
  574. }
  575. /*----------------------------------------------------------------------------*/
  576. void mt_gpio_dump(void)
  577. {
  578. GPIO_REGS *regs = (GPIO_REGS*)(GPIO_BASE);
  579. int idx;
  580. GPIOMSG("---# dir #-----------------------------------------------------------------\n");
  581. for (idx = 0; idx < sizeof(regs->dir)/sizeof(regs->dir[0]); idx++) {
  582. GPIOMSG("0x%04X ", regs->dir[idx].val);
  583. if (7 == (idx % 8)) GPIOMSG("\n");
  584. }
  585. GPIOMSG("\n---# pullen #--------------------------------------------------------------\n");
  586. for (idx = 0; idx < sizeof(regs->pullen)/sizeof(regs->pullen[0]); idx++) {
  587. GPIOMSG("0x%04X ", regs->pullen[idx].val);
  588. if (7 == (idx % 8)) GPIOMSG("\n");
  589. }
  590. GPIOMSG("\n---# pullsel #-------------------------------------------------------------\n");
  591. for (idx = 0; idx < sizeof(regs->pullsel)/sizeof(regs->pullsel[0]); idx++) {
  592. GPIOMSG("0x%04X ", regs->pullsel[idx].val);
  593. if (7 == (idx % 8)) GPIOMSG("\n");
  594. }
  595. GPIOMSG("\n---# dout #----------------------------------------------------------------\n");
  596. for (idx = 0; idx < sizeof(regs->dout)/sizeof(regs->dout[0]); idx++) {
  597. GPIOMSG("0x%04X ", regs->dout[idx].val);
  598. if (7 == (idx % 8)) GPIOMSG("\n");
  599. }
  600. GPIOMSG("\n---# din #----------------------------------------------------------------\n");
  601. for (idx = 0; idx < sizeof(regs->din)/sizeof(regs->din[0]); idx++) {
  602. GPIOMSG("0x%04X ", regs->din[idx].val);
  603. if (7 == (idx % 8)) GPIOMSG("\n");
  604. }
  605. GPIOMSG("\n---# mode #----------------------------------------------------------------\n");
  606. for (idx = 0; idx < sizeof(regs->mode)/sizeof(regs->mode[0]); idx++) {
  607. GPIOMSG("0x%04X ", regs->mode[idx].val);
  608. if (7 == (idx % 8)) GPIOMSG("\n");
  609. }
  610. GPIOMSG("sim0 0x%04X ", regs->sim_ctrl[0].val);
  611. GPIOMSG("sim1 0x%04X ", regs->sim_ctrl[1].val);
  612. GPIOMSG("sim2 0x%04X ", regs->sim_ctrl[2].val);
  613. GPIOMSG("sim3 0x%04X ", regs->sim_ctrl[3].val);
  614. GPIOMSG("keypad0 0x%04X ", regs->kpad_ctrl[0].val);
  615. GPIOMSG("keypad1 0x%04X ", regs->kpad_ctrl[1].val);
  616. GPIOMSG("\n---------------------------------------------------------------------------\n");
  617. }
  618. /*---------------------------------------------------------------------------*/
  619. void mt_gpio_read_pin(GPIO_CFG* cfg, int method)
  620. {
  621. if (method == 0) {
  622. GPIO_REGS *cur = (GPIO_REGS*)GPIO_BASE;
  623. u32 mask = (1L << GPIO_MODE_BITS) - 1;
  624. int num, bit,idx=cfg->no;
  625. num = idx / MAX_GPIO_REG_BITS;
  626. bit = idx % MAX_GPIO_REG_BITS;
  627. cfg->pullsel= (cur->pullsel[num].val & (1L << bit)) ? (1) : (0);
  628. cfg->din = (cur->din[num].val & (1L << bit)) ? (1) : (0);
  629. cfg->dout = (cur->dout[num].val & (1L << bit)) ? (1) : (0);
  630. cfg->pullen = (cur->pullen[num].val & (1L << bit)) ? (1) : (0);
  631. cfg->dir = (cur->dir[num].val & (1L << bit)) ? (1) : (0);
  632. num = idx / MAX_GPIO_MODE_PER_REG;
  633. bit = idx % MAX_GPIO_MODE_PER_REG;
  634. cfg->mode = (cur->mode[num].val >> (GPIO_MODE_BITS*bit)) & mask;
  635. } else if (method == 1) {
  636. int idx=cfg->no;
  637. cfg->pullsel= mt_get_gpio_pull_select(idx);
  638. cfg->din = mt_get_gpio_in(idx);
  639. cfg->dout = mt_get_gpio_out(idx);
  640. cfg->pullen = mt_get_gpio_pull_enable(idx);
  641. cfg->dir = mt_get_gpio_dir(idx);
  642. cfg->mode = mt_get_gpio_mode(idx);
  643. }
  644. }
  645. /*---------------------------------------------------------------------------*/
  646. void mt_gpio_dump_addr(void)
  647. {
  648. int idx;
  649. struct mt_gpio_obj *obj = gpio_obj;
  650. GPIO_REGS *reg = obj->reg;
  651. GPIOMSG("# direction\n");
  652. for (idx = 0; idx < sizeof(reg->dir)/sizeof(reg->dir[0]); idx++)
  653. GPIOMSG("val[%2d] %p\nset[%2d] %p\nrst[%2d] %p\n", idx, &reg->dir[idx].val, idx, &reg->dir[idx].set, idx, &reg->dir[idx].rst);
  654. GPIOMSG("# pull enable\n");
  655. for (idx = 0; idx < sizeof(reg->pullen)/sizeof(reg->pullen[0]); idx++)
  656. GPIOMSG("val[%2d] %p\nset[%2d] %p\nrst[%2d] %p\n", idx, &reg->pullen[idx].val, idx, &reg->pullen[idx].set, idx, &reg->pullen[idx].rst);
  657. GPIOMSG("# pull select\n");
  658. for (idx = 0; idx < sizeof(reg->pullsel)/sizeof(reg->pullsel[0]); idx++)
  659. GPIOMSG("val[%2d] %p\nset[%2d] %p\nrst[%2d] %p\n", idx, &reg->pullsel[idx].val, idx, &reg->pullsel[idx].set, idx, &reg->pullsel[idx].rst);
  660. GPIOMSG("# data output\n");
  661. for (idx = 0; idx < sizeof(reg->dout)/sizeof(reg->dout[0]); idx++)
  662. GPIOMSG("val[%2d] %p\nset[%2d] %p\nrst[%2d] %p\n", idx, &reg->dout[idx].val, idx, &reg->dout[idx].set, idx, &reg->dout[idx].rst);
  663. GPIOMSG("# data input\n");
  664. for (idx = 0; idx < sizeof(reg->din)/sizeof(reg->din[0]); idx++)
  665. GPIOMSG("val[%2d] %p\n", idx, &reg->din[idx].val);
  666. GPIOMSG("# mode\n");
  667. for (idx = 0; idx < sizeof(reg->mode)/sizeof(reg->mode[0]); idx++)
  668. GPIOMSG("val[%2d] %p\nset[%2d] %p\nrst[%2d] %p\n", idx, &reg->mode[idx].val, idx, &reg->mode[idx].set, idx, &reg->mode[idx].rst);
  669. }
  670. #endif
  671. #endif