mt_gpio.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2015. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. */
  31. #include <platform/mt_reg_base.h>
  32. #include <platform/mt_gpio.h>
  33. #include <platform/gpio_cfg.h>
  34. #include <debug.h>
  35. /******************************************************************************
  36. MACRO Definition
  37. ******************************************************************************/
  38. //#define GIO_SLFTEST
  39. #define GPIO_BRINGUP
  40. #define GPIO_DEVICE "mt-gpio"
  41. #define VERSION GPIO_DEVICE
  42. /*---------------------------------------------------------------------------*/
  43. #define SUPPORT_MODE_MWR
  44. #define REGSET (4)
  45. #define REGCLR (8)
  46. #define REGMWR (0xC)
  47. /*---------------------------------------------------------------------------*/
  48. #define GPIO_WR32(addr, data) DRV_WriteReg32(addr,data)
  49. #define GPIO_RD32(addr) DRV_Reg32(addr)
  50. #define GPIO_SET_BITS(REG, BIT) GPIO_WR32(REG + REGSET, BIT)
  51. #define GPIO_CLR_BITS(REG, BIT) GPIO_WR32(REG + REGCLR, BIT)
  52. /*---------------------------------------------------------------------------*/
  53. #define TRUE 1
  54. #define FALSE 0
  55. /*---------------------------------------------------------------------------*/
  56. #define GPIOTAG "[GPIO] "
  57. #define GPIOLOG(fmt, arg...) dprintf(INFO,GPIOTAG fmt, ##arg)
  58. #define GPIOMSG(fmt, arg...) dprintf(INFO,fmt, ##arg)
  59. #define GPIOERR(fmt, arg...) dprintf(INFO,GPIOTAG "%5d: "fmt, __LINE__, ##arg)
  60. #define GPIOFUC(fmt, arg...) //dprintk(INFO,GPIOTAG "%s\n", __FUNCTION__)
  61. #define GIO_INVALID_OBJ(ptr) ((ptr) != gpio_obj)
  62. /******************************************************************************
  63. Enumeration/Structure
  64. ******************************************************************************/
  65. #ifdef FPGA_PLATFORM
  66. S32 mt_set_gpio_dir(u32 pin, u32 dir) {return RSUCCESS;}
  67. S32 mt_get_gpio_dir(u32 pin) {return GPIO_DIR_UNSUPPORTED;}
  68. S32 mt_set_gpio_pull_enable(u32 pin, u32 en) {return RSUCCESS;}
  69. S32 mt_get_gpio_pull_enable(u32 pin) {return GPIO_PULL_EN_UNSUPPORTED;}
  70. S32 mt_set_gpio_pull_select(u32 pin, u32 sel) {return RSUCCESS;}
  71. S32 mt_get_gpio_pull_select(u32 pin) {return GPIO_PULL_UNSUPPORTED;}
  72. S32 mt_set_gpio_smt(u32 pin, u32 enable) {return RSUCCESS;}
  73. S32 mt_get_gpio_smt(u32 pin) {return GPIO_SMT_UNSUPPORTED;}
  74. S32 mt_set_gpio_ies(u32 pin, u32 enable) {return RSUCCESS;}
  75. S32 mt_get_gpio_ies(u32 pin) {return GPIO_IES_UNSUPPORTED;}
  76. S32 mt_set_gpio_out(u32 pin, u32 output) {return RSUCCESS;}
  77. S32 mt_get_gpio_out(u32 pin) {return GPIO_OUT_UNSUPPORTED;}
  78. S32 mt_get_gpio_in(u32 pin) {return GPIO_IN_UNSUPPORTED;}
  79. S32 mt_set_gpio_mode(u32 pin, u32 mode) {return RSUCCESS;}
  80. S32 mt_get_gpio_mode(u32 pin) {return GPIO_MODE_UNSUPPORTED;}
  81. #else
  82. struct mt_gpio_obj {
  83. GPIO_REGS *reg;
  84. };
  85. static struct mt_gpio_obj gpio_dat = {
  86. .reg = (GPIO_REGS*)(GPIO_BASE),
  87. };
  88. static struct mt_gpio_obj *gpio_obj = &gpio_dat;
  89. /*---------------------------------------------------------------------------*/
  90. S32 mt_set_gpio_dir_chip(u32 pin, u32 dir)
  91. {
  92. u32 pos;
  93. u32 bit;
  94. u32 reg;
  95. struct mt_gpio_obj *obj = gpio_obj;
  96. if (!obj)
  97. return -ERACCESS;
  98. if (pin >= MAX_GPIO_PIN)
  99. return -ERINVAL;
  100. if (dir >= GPIO_DIR_MAX)
  101. return -ERINVAL;
  102. pos = pin / MAX_GPIO_REG_BITS;
  103. bit = pin % MAX_GPIO_REG_BITS;
  104. /* To reduce memory usage, we don't use DIR_addr[] array*/
  105. if (dir == GPIO_DIR_IN)
  106. GPIO_WR32(&obj->reg->dir[pos].rst, 1L << bit);
  107. else
  108. GPIO_WR32(&obj->reg->dir[pos].set, 1L << bit);
  109. return RSUCCESS;
  110. }
  111. /*---------------------------------------------------------------------------*/
  112. S32 mt_get_gpio_dir_chip(u32 pin)
  113. {
  114. u32 pos;
  115. u32 bit;
  116. u32 reg;
  117. struct mt_gpio_obj *obj = gpio_obj;
  118. if (!obj)
  119. return -ERACCESS;
  120. if (pin >= MAX_GPIO_PIN)
  121. return -ERINVAL;
  122. pos = pin / MAX_GPIO_REG_BITS;
  123. bit = pin % MAX_GPIO_REG_BITS;
  124. /* To reduce memory usage, we don't use DIR_addr[] array*/
  125. reg = GPIO_RD32(&obj->reg->dir[pos].val);
  126. return (((reg & (1L << bit)) != 0)? 1: 0);
  127. }
  128. /*---------------------------------------------------------------------------*/
  129. S32 mt_set_gpio_out_chip(u32 pin, u32 output)
  130. {
  131. u32 pos;
  132. u32 bit;
  133. u32 reg;
  134. struct mt_gpio_obj *obj = gpio_obj;
  135. if (!obj)
  136. return -ERACCESS;
  137. if (pin >= MAX_GPIO_PIN)
  138. return -ERINVAL;
  139. if (output >= GPIO_OUT_MAX)
  140. return -ERINVAL;
  141. pos = pin / MAX_GPIO_REG_BITS;
  142. bit = pin % MAX_GPIO_REG_BITS;
  143. /* To reduce memory usage, we don't use DATAOUT_addr[] array*/
  144. if (output == GPIO_OUT_ZERO)
  145. GPIO_WR32(&obj->reg->dout[pos].rst, 1L << bit);
  146. else
  147. GPIO_WR32(&obj->reg->dout[pos].set, 1L << bit);
  148. return RSUCCESS;
  149. }
  150. /*---------------------------------------------------------------------------*/
  151. S32 mt_get_gpio_out_chip(u32 pin)
  152. {
  153. u32 pos;
  154. u32 bit;
  155. u32 reg;
  156. struct mt_gpio_obj *obj = gpio_obj;
  157. if (!obj)
  158. return -ERACCESS;
  159. if (pin >= MAX_GPIO_PIN)
  160. return -ERINVAL;
  161. pos = pin / MAX_GPIO_REG_BITS;
  162. bit = pin % MAX_GPIO_REG_BITS;
  163. /* To reduce memory usage, we don't use DATAOUT_addr[] array*/
  164. reg = GPIO_RD32(&obj->reg->dout[pos].val);
  165. return (((reg & (1L << bit)) != 0)? 1: 0);
  166. }
  167. /*---------------------------------------------------------------------------*/
  168. S32 mt_get_gpio_in_chip(u32 pin)
  169. {
  170. u32 pos;
  171. u32 bit;
  172. u32 reg;
  173. struct mt_gpio_obj *obj = gpio_obj;
  174. if (!obj)
  175. return -ERACCESS;
  176. if (pin >= MAX_GPIO_PIN)
  177. return -ERINVAL;
  178. pos = pin / MAX_GPIO_REG_BITS;
  179. bit = pin % MAX_GPIO_REG_BITS;
  180. /* To reduce memory usage, we don't use DIN_addr[] array*/
  181. reg = GPIO_RD32(&obj->reg->din[pos].val);
  182. return (((reg & (1L << bit)) != 0)? 1: 0);
  183. }
  184. /*---------------------------------------------------------------------------*/
  185. S32 mt_set_gpio_mode_chip(u32 pin, u32 mode)
  186. {
  187. unsigned long pos;
  188. unsigned long bit;
  189. unsigned long data;
  190. struct mt_gpio_obj *obj = gpio_obj;
  191. u32 mask;
  192. if (!obj)
  193. return -ERACCESS;
  194. if (pin >= MAX_GPIO_PIN)
  195. return -ERINVAL;
  196. if (mode >= GPIO_MODE_MAX)
  197. return -ERINVAL;
  198. #ifdef SUPPORT_MODE_MWR
  199. mask = (1L << (GPIO_MODE_BITS - 1)) - 1;
  200. #else
  201. mask = (1L << GPIO_MODE_BITS) - 1;
  202. #endif
  203. mode = mode & mask;
  204. pos = pin / MAX_GPIO_MODE_PER_REG;
  205. bit = (pin % MAX_GPIO_MODE_PER_REG) * GPIO_MODE_BITS;
  206. #ifdef SUPPORT_MODE_MWR
  207. data = (1L << (bit + GPIO_MODE_BITS - 1)) | (mode << bit);
  208. GPIO_WR32(&obj->reg->mode[pos]._align1, data);
  209. #else
  210. data = GPIO_RD32(&reg->mode[pos].val);
  211. data &= (~(mask << bit));
  212. data |= (mode << bit);
  213. GPIO_WR32(&obj->reg->mode[pos].val, data);
  214. #endif
  215. return RSUCCESS;
  216. }
  217. /*---------------------------------------------------------------------------*/
  218. S32 mt_get_gpio_mode_chip(u32 pin)
  219. {
  220. unsigned long pos;
  221. unsigned long bit;
  222. unsigned long data;
  223. u32 mask;
  224. struct mt_gpio_obj *obj = gpio_obj;
  225. if (!obj)
  226. return -ERACCESS;
  227. if (pin >= MAX_GPIO_PIN)
  228. return -ERINVAL;
  229. #ifdef SUPPORT_MODE_MWR
  230. mask = (1L << (GPIO_MODE_BITS - 1)) - 1;
  231. #else
  232. mask = (1L << GPIO_MODE_BITS) - 1;
  233. #endif
  234. pos = pin / MAX_GPIO_MODE_PER_REG;
  235. bit = (pin % MAX_GPIO_MODE_PER_REG) * GPIO_MODE_BITS;
  236. data = GPIO_RD32(&obj->reg->mode[pos].val);
  237. return (data >> bit) & mask;
  238. }
  239. /*---------------------------------------------------------------------------*/
  240. S32 mt_set_gpio_smt_chip(u32 pin, u32 enable)
  241. {
  242. if (pin >= MAX_GPIO_PIN)
  243. return -ERINVAL;
  244. if (SMT_offset[pin].offset == (S8)-1)
  245. return GPIO_SMT_UNSUPPORTED;
  246. if (enable == GPIO_SMT_DISABLE)
  247. GPIO_CLR_BITS(SMT_addr[pin].addr, 1L << SMT_offset[pin].offset);
  248. else
  249. GPIO_SET_BITS(SMT_addr[pin].addr, 1L << SMT_offset[pin].offset);
  250. return RSUCCESS;
  251. }
  252. /*---------------------------------------------------------------------------*/
  253. S32 mt_get_gpio_smt_chip(u32 pin)
  254. {
  255. u32 reg;
  256. if (pin >= MAX_GPIO_PIN)
  257. return -ERINVAL;
  258. if (SMT_offset[pin].offset == (s8)-1)
  259. return GPIO_SMT_UNSUPPORTED;
  260. reg = GPIO_RD32(SMT_addr[pin].addr);
  261. return ((reg & (1L << SMT_offset[pin].offset)) ? 1 : 0);
  262. }
  263. /*---------------------------------------------------------------------------*/
  264. S32 mt_set_gpio_ies_chip(u32 pin, u32 enable)
  265. {
  266. if (pin >= MAX_GPIO_PIN)
  267. return -ERINVAL;
  268. if (IES_offset[pin].offset == (s8)-1)
  269. return GPIO_IES_UNSUPPORTED;
  270. if (enable == GPIO_IES_DISABLE)
  271. GPIO_CLR_BITS(IES_addr[pin].addr, 1L << IES_offset[pin].offset);
  272. else
  273. GPIO_SET_BITS(IES_addr[pin].addr, 1L << IES_offset[pin].offset);
  274. return RSUCCESS;
  275. }
  276. /*---------------------------------------------------------------------------*/
  277. S32 mt_get_gpio_ies_chip(u32 pin)
  278. {
  279. u32 reg;
  280. if (pin >= MAX_GPIO_PIN)
  281. return -ERINVAL;
  282. if (IES_offset[pin].offset == (S8)-1)
  283. return GPIO_IES_UNSUPPORTED;
  284. reg = GPIO_RD32(IES_addr[pin].addr);
  285. return ((reg & (1L << IES_offset[pin].offset)) ? 1 : 0);
  286. }
  287. /*---------------------------------------------------------------------------*/
  288. S32 mt_set_gpio_pull_enable_chip(u32 pin, u32 en)
  289. {
  290. if (pin >= MAX_GPIO_PIN)
  291. return -ERINVAL;
  292. if ((PULLEN_offset[pin].offset == (S8)-1) && (PUPD_offset[pin].offset == (S8)-1))
  293. return GPIO_PULL_EN_UNSUPPORTED;
  294. if (en == GPIO_PULL_DISABLE) {
  295. if (PULLEN_offset[pin].offset == (S8)-1)
  296. GPIO_CLR_BITS(PUPD_addr[pin].addr, 3L << PUPD_offset[pin].offset);
  297. else
  298. GPIO_CLR_BITS(PULLEN_addr[pin].addr, 1L << PULLEN_offset[pin].offset);
  299. } else if (en == GPIO_PULL_ENABLE) {
  300. if (PULLEN_offset[pin].offset == (S8)-1)
  301. return GPIO_PULL_EN_UNSUPPORTED;
  302. GPIO_SET_BITS(PULLEN_addr[pin].addr, 1L << PULLEN_offset[pin].offset);
  303. } else if (en == GPIO_PULL_ENABLE_R0) {
  304. if (PUPD_offset[pin].offset == (S8)-1)
  305. return GPIO_PULL_EN_UNSUPPORTED;
  306. /* IOConfig does not support MWR operation, so use CLR + SET */
  307. GPIO_SET_BITS(PUPD_addr[pin].addr, 1L << PUPD_offset[pin].offset);
  308. GPIO_CLR_BITS(PUPD_addr[pin].addr, 1L << (PUPD_offset[pin].offset + 1));
  309. } else if (en == GPIO_PULL_ENABLE_R1) {
  310. if (PUPD_offset[pin].offset == (S8)-1)
  311. return GPIO_PULL_EN_UNSUPPORTED;
  312. /* IOConfig does not support MWR operation, so use CLR + SET */
  313. GPIO_CLR_BITS(PUPD_addr[pin].addr, 1L << PUPD_offset[pin].offset);
  314. GPIO_SET_BITS(PUPD_addr[pin].addr, 1L << (PUPD_offset[pin].offset + 1));
  315. } else if (en == GPIO_PULL_ENABLE_R0R1) {
  316. if (PUPD_offset[pin].offset == (S8)-1)
  317. return GPIO_PULL_EN_UNSUPPORTED;
  318. GPIO_SET_BITS(PUPD_addr[pin].addr, 3L << PUPD_offset[pin].offset);
  319. } else {
  320. return -ERINVAL;
  321. }
  322. return RSUCCESS;
  323. }
  324. /*---------------------------------------------------------------------------*/
  325. S32 mt_get_gpio_pull_enable_chip(u32 pin)
  326. {
  327. u32 reg;
  328. if (pin >= MAX_GPIO_PIN)
  329. return -ERINVAL;
  330. if ((PULLEN_offset[pin].offset == (S8)-1) && (PUPD_offset[pin].offset == (S8)-1))
  331. return GPIO_PULL_EN_UNSUPPORTED;
  332. if (PULLEN_offset[pin].offset == (S8)-1) {
  333. reg = GPIO_RD32(PUPD_addr[pin].addr);
  334. return ((reg & (3L << PUPD_offset[pin].offset)) ? 1 : 0);
  335. } else if (PUPD_offset[pin].offset == (S8)-1) {
  336. reg = GPIO_RD32(PULLEN_addr[pin].addr);
  337. return ((reg & (1L << PULLEN_offset[pin].offset)) ? 1 : 0);
  338. }
  339. return -ERINVAL;
  340. }
  341. /*---------------------------------------------------------------------------*/
  342. S32 mt_set_gpio_pull_select_chip(u32 pin, u32 sel)
  343. {
  344. if (pin >= MAX_GPIO_PIN)
  345. return -ERINVAL;
  346. if ((PULLSEL_offset[pin].offset == (S8)-1) && (PUPD_offset[pin].offset == (S8)-1))
  347. return GPIO_PULL_UNSUPPORTED;
  348. if (sel == GPIO_NO_PULL) {
  349. /* Regard No PULL as PULL disable + pull down */
  350. mt_set_gpio_pull_enable_chip(pin, GPIO_PULL_DISABLE);
  351. if (PULLSEL_offset[pin].offset == (S8)-1)
  352. GPIO_SET_BITS(PUPD_addr[pin].addr, 1L << (PUPD_offset[pin].offset + 2));
  353. else
  354. GPIO_CLR_BITS(PULLSEL_addr[pin].addr, 1L << PULLSEL_offset[pin].offset);
  355. } else if (sel == GPIO_PULL_UP) {
  356. if (PULLSEL_offset[pin].offset == (S8)-1)
  357. GPIO_CLR_BITS(PUPD_addr[pin].addr, 1L << (PUPD_offset[pin].offset + 2));
  358. else
  359. GPIO_SET_BITS(PULLSEL_addr[pin].addr, 1L << PULLSEL_offset[pin].offset);
  360. } else if (sel == GPIO_PULL_DOWN) {
  361. if (PULLSEL_offset[pin].offset == (S8)-1)
  362. GPIO_SET_BITS(PUPD_addr[pin].addr, 1L << (PUPD_offset[pin].offset + 2));
  363. else
  364. GPIO_CLR_BITS(PULLSEL_addr[pin].addr, 1L << PULLSEL_offset[pin].offset);
  365. } else {
  366. return -ERINVAL;
  367. }
  368. return RSUCCESS;
  369. }
  370. /*---------------------------------------------------------------------------*/
  371. /* get pull-up or pull-down, regardless of resistor value */
  372. S32 mt_get_gpio_pull_select_chip(u32 pin)
  373. {
  374. u32 reg;
  375. if (pin >= MAX_GPIO_PIN)
  376. return -ERINVAL;
  377. if ((PULLSEL_offset[pin].offset == (S8)-1) && (PUPD_offset[pin].offset == (S8)-1))
  378. return GPIO_PULL_UNSUPPORTED;
  379. if (PULLSEL_offset[pin].offset == (S8)-1) {
  380. reg = GPIO_RD32(PUPD_addr[pin].addr);
  381. return ((reg & (1L << (PUPD_offset[pin].offset + 2))) ? 1 : 0);
  382. } else if (PUPD_offset[pin].offset == (S8)-1) {
  383. reg = GPIO_RD32(PULLSEL_addr[pin].addr);
  384. return ((reg & (1L << PULLSEL_offset[pin].offset)) ? 1 : 0);
  385. }
  386. return -ERINVAL;
  387. }
  388. /*---------------------------------------------------------------------------*/
  389. void mt_gpio_pin_decrypt(u32 *cipher)
  390. {
  391. //just for debug, find out who used pin number directly
  392. if ((*cipher & (0x80000000)) == 0) {
  393. GPIOERR("Pin %d decrypt warning! \n",*cipher);
  394. //dump_stack();
  395. //return;
  396. }
  397. //GPIOERR("Pin magic number is %x\n",*cipher);
  398. *cipher &= ~(0x80000000);
  399. return;
  400. }
  401. /*---------------------------------------------------------------------------*/
  402. S32 mt_set_gpio_dir(u32 pin, u32 dir)
  403. {
  404. mt_gpio_pin_decrypt(&pin);
  405. return mt_set_gpio_dir_chip(pin,dir);
  406. }
  407. /*---------------------------------------------------------------------------*/
  408. S32 mt_get_gpio_dir(u32 pin)
  409. {
  410. mt_gpio_pin_decrypt(&pin);
  411. return mt_get_gpio_dir_chip(pin);
  412. }
  413. /*---------------------------------------------------------------------------*/
  414. S32 mt_set_gpio_pull_enable(u32 pin, u32 enable)
  415. {
  416. mt_gpio_pin_decrypt(&pin);
  417. return mt_set_gpio_pull_enable_chip(pin,enable);
  418. }
  419. /*---------------------------------------------------------------------------*/
  420. S32 mt_get_gpio_pull_enable(u32 pin)
  421. {
  422. mt_gpio_pin_decrypt(&pin);
  423. return mt_get_gpio_pull_enable_chip(pin);
  424. }
  425. /*---------------------------------------------------------------------------*/
  426. S32 mt_set_gpio_pull_select(u32 pin, u32 select)
  427. {
  428. mt_gpio_pin_decrypt(&pin);
  429. return mt_set_gpio_pull_select_chip(pin,select);
  430. }
  431. /*---------------------------------------------------------------------------*/
  432. S32 mt_get_gpio_pull_select(u32 pin)
  433. {
  434. mt_gpio_pin_decrypt(&pin);
  435. return mt_get_gpio_pull_select_chip(pin);
  436. }
  437. /*---------------------------------------------------------------------------*/
  438. S32 mt_set_gpio_smt(u32 pin, u32 enable)
  439. {
  440. mt_gpio_pin_decrypt(&pin);
  441. return mt_set_gpio_smt_chip(pin,enable);
  442. }
  443. /*---------------------------------------------------------------------------*/
  444. S32 mt_get_gpio_smt(u32 pin)
  445. {
  446. mt_gpio_pin_decrypt(&pin);
  447. return mt_get_gpio_smt_chip(pin);
  448. }
  449. /*---------------------------------------------------------------------------*/
  450. S32 mt_set_gpio_ies(u32 pin, u32 enable)
  451. {
  452. mt_gpio_pin_decrypt(&pin);
  453. return mt_set_gpio_ies_chip(pin,enable);
  454. }
  455. /*---------------------------------------------------------------------------*/
  456. S32 mt_get_gpio_ies(u32 pin)
  457. {
  458. mt_gpio_pin_decrypt(&pin);
  459. return mt_get_gpio_ies_chip(pin);
  460. }
  461. /*---------------------------------------------------------------------------*/
  462. S32 mt_set_gpio_out(u32 pin, u32 output)
  463. {
  464. mt_gpio_pin_decrypt(&pin);
  465. return mt_set_gpio_out_chip(pin,output);
  466. }
  467. /*---------------------------------------------------------------------------*/
  468. S32 mt_get_gpio_out(u32 pin)
  469. {
  470. mt_gpio_pin_decrypt(&pin);
  471. return mt_get_gpio_out_chip(pin);
  472. }
  473. /*---------------------------------------------------------------------------*/
  474. S32 mt_get_gpio_in(u32 pin)
  475. {
  476. mt_gpio_pin_decrypt(&pin);
  477. return mt_get_gpio_in_chip(pin);
  478. }
  479. /*---------------------------------------------------------------------------*/
  480. S32 mt_set_gpio_mode(u32 pin, u32 mode)
  481. {
  482. mt_gpio_pin_decrypt(&pin);
  483. return mt_set_gpio_mode_chip(pin,mode);
  484. }
  485. /*---------------------------------------------------------------------------*/
  486. S32 mt_get_gpio_mode(u32 pin)
  487. {
  488. mt_gpio_pin_decrypt(&pin);
  489. return mt_get_gpio_mode_chip(pin);
  490. }
  491. /*****************************************************************************/
  492. /* Self test operation */
  493. /*****************************************************************************/
  494. #if GPIO_SELF_TEST
  495. void mt_gpio_self_test(void)
  496. {
  497. int i, val;
  498. for (i = 0; i < GPIO_MAX; i++) {
  499. S32 res,old;
  500. GPIOMSG("GPIO-%3d test\n", i);
  501. /*direction test*/
  502. old = mt_get_gpio_dir(i);
  503. if (old == 0 || old == 1) {
  504. GPIOLOG(" dir old = %d\n", old);
  505. } else {
  506. GPIOERR(" test dir fail: %d\n", old);
  507. break;
  508. }
  509. if ((res = mt_set_gpio_dir(i, GPIO_DIR_OUT)) != RSUCCESS) {
  510. GPIOERR(" set dir out fail: %d\n", res);
  511. break;
  512. } else if ((res = mt_get_gpio_dir(i)) != GPIO_DIR_OUT) {
  513. GPIOERR(" get dir out fail: %d\n", res);
  514. break;
  515. } else {
  516. /*output test*/
  517. S32 out = mt_get_gpio_out(i);
  518. if (out != 0 && out != 1) {
  519. GPIOERR(" get out fail = %d\n", old);
  520. break;
  521. }
  522. for (val = 0; val < GPIO_OUT_MAX; val++) {
  523. if ((res = mt_set_gpio_out(i,0)) != RSUCCESS) {
  524. GPIOERR(" set out[%d] fail: %d\n", val, res);
  525. break;
  526. } else if ((res = mt_get_gpio_out(i)) != 0) {
  527. GPIOERR(" get out[%d] fail: %d\n", val, res);
  528. break;
  529. }
  530. }
  531. if ((res = mt_set_gpio_out(i,out)) != RSUCCESS) {
  532. GPIOERR(" restore out fail: %d\n", res);
  533. break;
  534. }
  535. }
  536. if ((res = mt_set_gpio_dir(i, GPIO_DIR_IN)) != RSUCCESS) {
  537. GPIOERR(" set dir in fail: %d\n", res);
  538. break;
  539. } else if ((res = mt_get_gpio_dir(i)) != GPIO_DIR_IN) {
  540. GPIOERR(" get dir in fail: %d\n", res);
  541. break;
  542. } else {
  543. GPIOLOG(" input data = %d\n", res);
  544. }
  545. if ((res = mt_set_gpio_dir(i, old)) != RSUCCESS) {
  546. GPIOERR(" restore dir fail: %d\n", res);
  547. break;
  548. }
  549. for (val = 0; val < GPIO_PULL_EN_MAX; val++) {
  550. if ((res = mt_set_gpio_pull_enable(i,val)) != RSUCCESS) {
  551. GPIOERR(" set pullen[%d] fail: %d\n", val, res);
  552. break;
  553. } else if ((res = mt_get_gpio_pull_enable(i)) != val) {
  554. GPIOERR(" get pullen[%d] fail: %d\n", val, res);
  555. break;
  556. }
  557. }
  558. if ((res = mt_set_gpio_pull_enable(i, old)) != RSUCCESS) {
  559. GPIOERR(" restore pullen fail: %d\n", res);
  560. break;
  561. }
  562. /*pull select test*/
  563. old = mt_get_gpio_pull_select(i);
  564. if (old == 0 || old == 1)
  565. GPIOLOG(" pullsel old = %d\n", old);
  566. else {
  567. GPIOERR(" pullsel fail: %d\n", old);
  568. break;
  569. }
  570. for (val = 0; val < GPIO_PULL_MAX; val++) {
  571. if ((res = mt_set_gpio_pull_select(i,val)) != RSUCCESS) {
  572. GPIOERR(" set pullsel[%d] fail: %d\n", val, res);
  573. break;
  574. } else if ((res = mt_get_gpio_pull_select(i)) != val) {
  575. GPIOERR(" get pullsel[%d] fail: %d\n", val, res);
  576. break;
  577. }
  578. }
  579. if ((res = mt_set_gpio_pull_select(i, old)) != RSUCCESS) {
  580. GPIOERR(" restore pullsel fail: %d\n", res);
  581. break;
  582. }
  583. /*mode control*/
  584. old = mt_get_gpio_mode(i);
  585. if ((old >= GPIO_MODE_00) && (val < GPIO_MODE_MAX)) {
  586. GPIOLOG(" mode old = %d\n", old);
  587. } else {
  588. GPIOERR(" get mode fail: %d\n", old);
  589. break;
  590. }
  591. for (val = 0; val < GPIO_MODE_MAX; val++) {
  592. if ((res = mt_set_gpio_mode(i, val)) != RSUCCESS) {
  593. GPIOERR("set mode[%d] fail: %d\n", val, res);
  594. break;
  595. } else if ((res = mt_get_gpio_mode(i)) != val) {
  596. GPIOERR("get mode[%d] fail: %d\n", val, res);
  597. break;
  598. }
  599. }
  600. if ((res = mt_set_gpio_mode(i,old)) != RSUCCESS) {
  601. GPIOERR(" restore mode fail: %d\n", res);
  602. break;
  603. }
  604. }
  605. GPIOLOG("GPIO test done\n");
  606. }
  607. /*----------------------------------------------------------------------------*/
  608. void mt_gpio_dump(void)
  609. {
  610. GPIO_REGS *regs = (GPIO_REGS*)(GPIO_BASE);
  611. int idx;
  612. GPIOMSG("---# dir #-----------------------------------------------------------------\n");
  613. for (idx = 0; idx < sizeof(regs->dir)/sizeof(regs->dir[0]); idx++) {
  614. GPIOMSG("0x%04X ", regs->dir[idx].val);
  615. if (7 == (idx % 8)) GPIOMSG("\n");
  616. }
  617. GPIOMSG("\n---# pullen #--------------------------------------------------------------\n");
  618. for (idx = 0; idx < sizeof(regs->pullen)/sizeof(regs->pullen[0]); idx++) {
  619. GPIOMSG("0x%04X ", regs->pullen[idx].val);
  620. if (7 == (idx % 8)) GPIOMSG("\n");
  621. }
  622. GPIOMSG("\n---# pullsel #-------------------------------------------------------------\n");
  623. for (idx = 0; idx < sizeof(regs->pullsel)/sizeof(regs->pullsel[0]); idx++) {
  624. GPIOMSG("0x%04X ", regs->pullsel[idx].val);
  625. if (7 == (idx % 8)) GPIOMSG("\n");
  626. }
  627. GPIOMSG("\n---# dout #----------------------------------------------------------------\n");
  628. for (idx = 0; idx < sizeof(regs->dout)/sizeof(regs->dout[0]); idx++) {
  629. GPIOMSG("0x%04X ", regs->dout[idx].val);
  630. if (7 == (idx % 8)) GPIOMSG("\n");
  631. }
  632. GPIOMSG("\n---# din #----------------------------------------------------------------\n");
  633. for (idx = 0; idx < sizeof(regs->din)/sizeof(regs->din[0]); idx++) {
  634. GPIOMSG("0x%04X ", regs->din[idx].val);
  635. if (7 == (idx % 8)) GPIOMSG("\n");
  636. }
  637. GPIOMSG("\n---# mode #----------------------------------------------------------------\n");
  638. for (idx = 0; idx < sizeof(regs->mode)/sizeof(regs->mode[0]); idx++) {
  639. GPIOMSG("0x%04X ", regs->mode[idx].val);
  640. if (7 == (idx % 8)) GPIOMSG("\n");
  641. }
  642. GPIOMSG("sim0 0x%04X ", regs->sim_ctrl[0].val);
  643. GPIOMSG("sim1 0x%04X ", regs->sim_ctrl[1].val);
  644. GPIOMSG("sim2 0x%04X ", regs->sim_ctrl[2].val);
  645. GPIOMSG("sim3 0x%04X ", regs->sim_ctrl[3].val);
  646. GPIOMSG("keypad0 0x%04X ", regs->kpad_ctrl[0].val);
  647. GPIOMSG("keypad1 0x%04X ", regs->kpad_ctrl[1].val);
  648. GPIOMSG("\n---------------------------------------------------------------------------\n");
  649. }
  650. /*---------------------------------------------------------------------------*/
  651. void mt_gpio_read_pin(GPIO_CFG* cfg, int method)
  652. {
  653. if (method == 0) {
  654. GPIO_REGS *cur = (GPIO_REGS*)GPIO_BASE;
  655. u32 mask = (1L << GPIO_MODE_BITS) - 1;
  656. int num, bit,idx=cfg->no;
  657. num = idx / MAX_GPIO_REG_BITS;
  658. bit = idx % MAX_GPIO_REG_BITS;
  659. cfg->pullsel= (cur->pullsel[num].val & (1L << bit)) ? (1) : (0);
  660. cfg->din = (cur->din[num].val & (1L << bit)) ? (1) : (0);
  661. cfg->dout = (cur->dout[num].val & (1L << bit)) ? (1) : (0);
  662. cfg->pullen = (cur->pullen[num].val & (1L << bit)) ? (1) : (0);
  663. cfg->dir = (cur->dir[num].val & (1L << bit)) ? (1) : (0);
  664. num = idx / MAX_GPIO_MODE_PER_REG;
  665. bit = idx % MAX_GPIO_MODE_PER_REG;
  666. cfg->mode = (cur->mode[num].val >> (GPIO_MODE_BITS*bit)) & mask;
  667. } else if (method == 1) {
  668. int idx=cfg->no;
  669. cfg->pullsel= mt_get_gpio_pull_select(idx);
  670. cfg->din = mt_get_gpio_in(idx);
  671. cfg->dout = mt_get_gpio_out(idx);
  672. cfg->pullen = mt_get_gpio_pull_enable(idx);
  673. cfg->dir = mt_get_gpio_dir(idx);
  674. cfg->mode = mt_get_gpio_mode(idx);
  675. }
  676. }
  677. /*---------------------------------------------------------------------------*/
  678. void mt_gpio_dump_addr(void)
  679. {
  680. int idx;
  681. struct mt_gpio_obj *obj = gpio_obj;
  682. GPIO_REGS *reg = obj->reg;
  683. GPIOMSG("# direction\n");
  684. for (idx = 0; idx < sizeof(reg->dir)/sizeof(reg->dir[0]); idx++)
  685. 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);
  686. GPIOMSG("# pull enable\n");
  687. for (idx = 0; idx < sizeof(reg->pullen)/sizeof(reg->pullen[0]); idx++)
  688. 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);
  689. GPIOMSG("# pull select\n");
  690. for (idx = 0; idx < sizeof(reg->pullsel)/sizeof(reg->pullsel[0]); idx++)
  691. 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);
  692. GPIOMSG("# data output\n");
  693. for (idx = 0; idx < sizeof(reg->dout)/sizeof(reg->dout[0]); idx++)
  694. 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);
  695. GPIOMSG("# data input\n");
  696. for (idx = 0; idx < sizeof(reg->din)/sizeof(reg->din[0]); idx++)
  697. GPIOMSG("val[%2d] %p\n", idx, &reg->din[idx].val);
  698. GPIOMSG("# mode\n");
  699. for (idx = 0; idx < sizeof(reg->mode)/sizeof(reg->mode[0]); idx++)
  700. 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);
  701. }
  702. #endif
  703. #endif