gpio.c 28 KB

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