mt_gpio.c 22 KB

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