gpio.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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) 2020. 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...) //dprintk(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_dir(u32 pin, u32 dir) {return RSUCCESS;}
  73. S32 mt_get_gpio_dir(u32 pin) {return GPIO_DIR_UNSUPPORTED;}
  74. S32 mt_set_gpio_pull_enable(u32 pin, u32 en) {return RSUCCESS;}
  75. S32 mt_get_gpio_pull_enable(u32 pin) {return GPIO_PULL_EN_UNSUPPORTED;}
  76. S32 mt_set_gpio_pull_select(u32 pin, u32 sel) {return RSUCCESS;}
  77. S32 mt_get_gpio_pull_select(u32 pin) {return GPIO_PULL_UNSUPPORTED;}
  78. S32 mt_set_gpio_smt(u32 pin, u32 enable) {return RSUCCESS;}
  79. S32 mt_get_gpio_smt(u32 pin) {return GPIO_SMT_UNSUPPORTED;}
  80. S32 mt_set_gpio_ies(u32 pin, u32 enable) {return RSUCCESS;}
  81. S32 mt_get_gpio_ies(u32 pin) {return GPIO_IES_UNSUPPORTED;}
  82. S32 mt_set_gpio_out(u32 pin, u32 output) {return RSUCCESS;}
  83. S32 mt_get_gpio_out(u32 pin) {return GPIO_OUT_UNSUPPORTED;}
  84. S32 mt_get_gpio_in(u32 pin) {return GPIO_IN_UNSUPPORTED;}
  85. S32 mt_set_gpio_mode(u32 pin, u32 mode) {return RSUCCESS;}
  86. S32 mt_get_gpio_mode(u32 pin) {return GPIO_MODE_UNSUPPORTED;}
  87. S32 mt_set_gpio_drv(u32 pin, u32 drv) {return RSUCCESS;}
  88. S32 mt_get_gpio_drv(u32 pin) {return GPIO_DIR_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_dir_chip(u32 pin, u32 dir)
  99. {
  100. u32 pos;
  101. u32 bit;
  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 (dir >= GPIO_DIR_MAX)
  108. return -ERINVAL;
  109. pos = pin / MAX_GPIO_REG_BITS;
  110. bit = pin % MAX_GPIO_REG_BITS;
  111. /* To reduce memory usage, we don't use DIR_addr[] array*/
  112. if (dir == GPIO_DIR_IN)
  113. GPIO_WR32(&obj->reg->dir[pos].rst, 1UL << bit);
  114. else
  115. GPIO_WR32(&obj->reg->dir[pos].set, 1UL << bit);
  116. return RSUCCESS;
  117. }
  118. /*---------------------------------------------------------------------------*/
  119. S32 mt_get_gpio_dir_chip(u32 pin)
  120. {
  121. u32 pos;
  122. u32 bit;
  123. u32 reg;
  124. struct mt_gpio_obj *obj = gpio_obj;
  125. if (!obj)
  126. return -ERACCESS;
  127. if (pin >= MAX_GPIO_PIN)
  128. return -ERINVAL;
  129. pos = pin / MAX_GPIO_REG_BITS;
  130. bit = pin % MAX_GPIO_REG_BITS;
  131. /* To reduce memory usage, we don't use DIR_addr[] array*/
  132. reg = GPIO_RD32(&obj->reg->dir[pos].val);
  133. return (((reg & (1UL << bit)) != 0)? 1: 0);
  134. }
  135. /*---------------------------------------------------------------------------*/
  136. S32 mt_set_gpio_out_chip(u32 pin, u32 output)
  137. {
  138. u32 pos;
  139. u32 bit;
  140. struct mt_gpio_obj *obj = gpio_obj;
  141. if (!obj)
  142. return -ERACCESS;
  143. if (pin >= MAX_GPIO_PIN)
  144. return -ERINVAL;
  145. if (output >= GPIO_OUT_MAX)
  146. return -ERINVAL;
  147. pos = pin / MAX_GPIO_REG_BITS;
  148. bit = pin % MAX_GPIO_REG_BITS;
  149. /* To reduce memory usage, we don't use DATAOUT_addr[] array*/
  150. if (output == GPIO_OUT_ZERO)
  151. GPIO_WR32(&obj->reg->dout[pos].rst, 1UL << bit);
  152. else
  153. GPIO_WR32(&obj->reg->dout[pos].set, 1UL << bit);
  154. return RSUCCESS;
  155. }
  156. /*---------------------------------------------------------------------------*/
  157. S32 mt_get_gpio_out_chip(u32 pin)
  158. {
  159. u32 pos;
  160. u32 bit;
  161. u32 reg;
  162. struct mt_gpio_obj *obj = gpio_obj;
  163. if (!obj)
  164. return -ERACCESS;
  165. if (pin >= MAX_GPIO_PIN)
  166. return -ERINVAL;
  167. pos = pin / MAX_GPIO_REG_BITS;
  168. bit = pin % MAX_GPIO_REG_BITS;
  169. /* To reduce memory usage, we don't use DATAOUT_addr[] array*/
  170. reg = GPIO_RD32(&obj->reg->dout[pos].val);
  171. return (((reg & (1UL << bit)) != 0)? 1: 0);
  172. }
  173. /*---------------------------------------------------------------------------*/
  174. S32 mt_get_gpio_in_chip(u32 pin)
  175. {
  176. u32 pos;
  177. u32 bit;
  178. u32 reg;
  179. struct mt_gpio_obj *obj = gpio_obj;
  180. if (!obj)
  181. return -ERACCESS;
  182. if (pin >= MAX_GPIO_PIN)
  183. return -ERINVAL;
  184. pos = pin / MAX_GPIO_REG_BITS;
  185. bit = pin % MAX_GPIO_REG_BITS;
  186. /* To reduce memory usage, we don't use DIN_addr[] array*/
  187. reg = GPIO_RD32(&obj->reg->din[pos].val);
  188. return (((reg & (1UL << bit)) != 0)? 1: 0);
  189. }
  190. /*---------------------------------------------------------------------------*/
  191. S32 mt_set_gpio_mode_chip(u32 pin, u32 mode)
  192. {
  193. unsigned long pos;
  194. unsigned long bit;
  195. unsigned long data;
  196. struct mt_gpio_obj *obj = gpio_obj;
  197. u32 mask;
  198. if (!obj)
  199. return -ERACCESS;
  200. if (pin >= MAX_GPIO_PIN)
  201. return -ERINVAL;
  202. if (mode >= GPIO_MODE_MAX)
  203. return -ERINVAL;
  204. #ifdef SUPPORT_MODE_MWR
  205. mask = (1UL << (GPIO_MODE_BITS - 1)) - 1;
  206. #else
  207. mask = (1UL << GPIO_MODE_BITS) - 1;
  208. #endif
  209. mode = mode & mask;
  210. pos = pin / MAX_GPIO_MODE_PER_REG;
  211. bit = (pin % MAX_GPIO_MODE_PER_REG) * GPIO_MODE_BITS;
  212. #ifdef SUPPORT_MODE_MWR
  213. data = (1UL << (bit + GPIO_MODE_BITS - 1)) | (mode << bit);
  214. GPIO_WR32(&obj->reg->mode[pos]._align1, data);
  215. #else
  216. data = GPIO_RD32(&reg->mode[pos].val);
  217. data &= (~(mask << bit));
  218. data |= (mode << bit);
  219. GPIO_WR32(&obj->reg->mode[pos].val, data);
  220. #endif
  221. return RSUCCESS;
  222. }
  223. /*---------------------------------------------------------------------------*/
  224. S32 mt_get_gpio_mode_chip(u32 pin)
  225. {
  226. unsigned long pos;
  227. unsigned long bit;
  228. unsigned long data;
  229. u32 mask;
  230. struct mt_gpio_obj *obj = gpio_obj;
  231. if (!obj)
  232. return -ERACCESS;
  233. if (pin >= MAX_GPIO_PIN)
  234. return -ERINVAL;
  235. #ifdef SUPPORT_MODE_MWR
  236. mask = (1UL << (GPIO_MODE_BITS - 1)) - 1;
  237. #else
  238. mask = (1UL << GPIO_MODE_BITS) - 1;
  239. #endif
  240. pos = pin / MAX_GPIO_MODE_PER_REG;
  241. bit = (pin % MAX_GPIO_MODE_PER_REG) * GPIO_MODE_BITS;
  242. data = GPIO_RD32(&obj->reg->mode[pos].val);
  243. return (data >> bit) & mask;
  244. }
  245. /*---------------------------------------------------------------------------*/
  246. S32 mt_set_gpio_smt_chip(u32 pin, u32 enable)
  247. {
  248. if (pin >= MAX_GPIO_PIN)
  249. return -ERINVAL;
  250. if (SMT_offset[pin].offset == (S8)-1)
  251. return GPIO_SMT_UNSUPPORTED;
  252. if (enable == GPIO_SMT_DISABLE)
  253. GPIO_CLR_BITS(SMT_addr[pin].addr, 1UL << SMT_offset[pin].offset);
  254. else
  255. GPIO_SET_BITS(SMT_addr[pin].addr, 1UL << SMT_offset[pin].offset);
  256. return RSUCCESS;
  257. }
  258. /*---------------------------------------------------------------------------*/
  259. S32 mt_get_gpio_smt_chip(u32 pin)
  260. {
  261. u32 reg;
  262. if (pin >= MAX_GPIO_PIN)
  263. return -ERINVAL;
  264. if (SMT_offset[pin].offset == (S8)-1)
  265. return GPIO_SMT_UNSUPPORTED;
  266. reg = GPIO_RD32(SMT_addr[pin].addr);
  267. return ((reg & (1UL << SMT_offset[pin].offset)) ? 1 : 0);
  268. }
  269. /*---------------------------------------------------------------------------*/
  270. S32 mt_set_gpio_ies_chip(u32 pin, u32 enable)
  271. {
  272. if (pin >= MAX_GPIO_PIN)
  273. return -ERINVAL;
  274. if (IES_offset[pin].offset == (S8)-1)
  275. return GPIO_IES_UNSUPPORTED;
  276. if (enable == GPIO_IES_DISABLE)
  277. GPIO_CLR_BITS(IES_addr[pin].addr, 1UL << IES_offset[pin].offset);
  278. else
  279. GPIO_SET_BITS(IES_addr[pin].addr, 1UL << IES_offset[pin].offset);
  280. return RSUCCESS;
  281. }
  282. /*---------------------------------------------------------------------------*/
  283. S32 mt_get_gpio_ies_chip(u32 pin)
  284. {
  285. u32 reg;
  286. if (pin >= MAX_GPIO_PIN)
  287. return -ERINVAL;
  288. if (IES_offset[pin].offset == (S8)-1)
  289. return GPIO_IES_UNSUPPORTED;
  290. reg = GPIO_RD32(IES_addr[pin].addr);
  291. return ((reg & (1UL << IES_offset[pin].offset)) ? 1 : 0);
  292. }
  293. /*---------------------------------------------------------------------------*/
  294. S32 mt_set_gpio_pull_enable_chip(u32 pin, u32 en)
  295. {
  296. if (pin >= MAX_GPIO_PIN)
  297. return -ERINVAL;
  298. if ((PU_offset[pin].offset == (S8)-1) && (PUPD_offset[pin].offset == (S8)-1))
  299. return GPIO_PULL_EN_UNSUPPORTED;
  300. if (en == GPIO_PULL_DISABLE) {
  301. if (PU_offset[pin].offset == (S8)-1) {
  302. GPIO_CLR_BITS(R0_addr[pin].addr, 1UL << R0_offset[pin].offset);
  303. GPIO_CLR_BITS(R1_addr[pin].addr, 1UL << R1_offset[pin].offset);
  304. } else {
  305. GPIO_CLR_BITS(PU_addr[pin].addr, 1UL << PU_offset[pin].offset);
  306. GPIO_CLR_BITS(PD_addr[pin].addr, 1UL << PD_offset[pin].offset);
  307. }
  308. } else if (en == GPIO_PULL_ENABLE) {
  309. if (PU_offset[pin].offset == (S8)-1) {
  310. /* For PUPD+R0+R1 Type, mt_set_gpio_pull_enable does not know
  311. * which one between PU and PD shall be enabled.
  312. * Use R0 to guarantee at one resistor is set when lk
  313. * apply default setting
  314. */
  315. GPIO_SET_BITS(R0_addr[pin].addr, 1UL << R0_offset[pin].offset);
  316. GPIO_CLR_BITS(R1_addr[pin].addr, 1UL << R1_offset[pin].offset);
  317. } else {
  318. /* For PU + PD Type, mt_set_gpio_pull_enable does not know
  319. * which one between PU and PD shall be enabled.
  320. * Use mt_set_gpio_pull_select() or GPIO_PULL_ENABLE_Rx instead.
  321. */
  322. return GPIO_PULL_EN_UNSUPPORTED;
  323. }
  324. } else if (en == GPIO_PULL_ENABLE_R0) {
  325. if (PUPD_offset[pin].offset == (S8)-1)
  326. return GPIO_PULL_EN_UNSUPPORTED;
  327. /* IOConfig does not support MWR operation, so use CLR + SET */
  328. GPIO_SET_BITS(R0_addr[pin].addr, 1UL << R0_offset[pin].offset);
  329. GPIO_CLR_BITS(R1_addr[pin].addr, 1UL << R1_offset[pin].offset);
  330. } else if (en == GPIO_PULL_ENABLE_R1) {
  331. if (PUPD_offset[pin].offset == (S8)-1)
  332. return GPIO_PULL_EN_UNSUPPORTED;
  333. /* IOConfig does not support MWR operation, so use CLR + SET */
  334. GPIO_CLR_BITS(R0_addr[pin].addr, 1UL << R0_offset[pin].offset);
  335. GPIO_SET_BITS(R1_addr[pin].addr, 1UL << R1_offset[pin].offset);
  336. } else if (en == GPIO_PULL_ENABLE_R0R1) {
  337. if (PUPD_offset[pin].offset == (S8)-1)
  338. return GPIO_PULL_EN_UNSUPPORTED;
  339. GPIO_SET_BITS(R0_addr[pin].addr, 1UL << R0_offset[pin].offset);
  340. GPIO_SET_BITS(R1_addr[pin].addr, 1UL << R1_offset[pin].offset);
  341. } else {
  342. return -ERINVAL;
  343. }
  344. return RSUCCESS;
  345. }
  346. /*---------------------------------------------------------------------------*/
  347. S32 mt_get_gpio_pull_enable_chip(u32 pin)
  348. {
  349. u32 reg, reg2;
  350. if (pin >= MAX_GPIO_PIN)
  351. return -ERINVAL;
  352. if ((PU_offset[pin].offset == (S8)-1) && (PUPD_offset[pin].offset == (S8)-1))
  353. return GPIO_PULL_EN_UNSUPPORTED;
  354. if (PU_offset[pin].offset == (S8)-1) {
  355. reg = (GPIO_RD32(R0_addr[pin].addr) >> R0_offset[pin].offset) & 1;
  356. reg2 = (GPIO_RD32(R1_addr[pin].addr) >> R1_offset[pin].offset) & 1;
  357. return ((reg | reg2) ? 1 : 0);
  358. } else if (PUPD_offset[pin].offset == (S8)-1) {
  359. reg = (GPIO_RD32(PU_addr[pin].addr) >> PU_offset[pin].offset) & 1;
  360. reg2 = (GPIO_RD32(PD_addr[pin].addr) >> PD_offset[pin].offset) & 1;
  361. return ((reg | reg2) ? 1 : 0);
  362. }
  363. return -ERINVAL;
  364. }
  365. /*---------------------------------------------------------------------------*/
  366. S32 mt_set_gpio_pull_select_chip(u32 pin, u32 sel)
  367. {
  368. if (pin >= MAX_GPIO_PIN)
  369. return -ERINVAL;
  370. if ((PU_offset[pin].offset == (S8)-1) && (PUPD_offset[pin].offset == (S8)-1))
  371. return GPIO_PULL_UNSUPPORTED;
  372. if (sel == GPIO_NO_PULL) {
  373. mt_set_gpio_pull_enable_chip(pin, GPIO_PULL_DISABLE);
  374. if (PU_offset[pin].offset == (S8)-1) {
  375. GPIO_SET_BITS(PUPD_addr[pin].addr, 1UL << PUPD_offset[pin].offset);
  376. } else {
  377. /* do nothting since already modified in mt_set_gpio_pull_enable_chip */
  378. }
  379. } else if (sel == GPIO_PULL_UP) {
  380. if (PU_offset[pin].offset == (S8)-1)
  381. GPIO_CLR_BITS(PUPD_addr[pin].addr, 1UL << PUPD_offset[pin].offset);
  382. else {
  383. GPIO_SET_BITS(PU_addr[pin].addr, 1UL << PU_offset[pin].offset);
  384. GPIO_CLR_BITS(PD_addr[pin].addr, 1UL << PD_offset[pin].offset);
  385. }
  386. } else if (sel == GPIO_PULL_DOWN) {
  387. if (PU_offset[pin].offset == (S8)-1)
  388. GPIO_SET_BITS(PUPD_addr[pin].addr, 1UL << PUPD_offset[pin].offset);
  389. else {
  390. GPIO_CLR_BITS(PU_addr[pin].addr, 1UL << PU_offset[pin].offset);
  391. GPIO_SET_BITS(PD_addr[pin].addr, 1UL << PD_offset[pin].offset);
  392. }
  393. } else {
  394. return -ERINVAL;
  395. }
  396. return RSUCCESS;
  397. }
  398. /*---------------------------------------------------------------------------*/
  399. /* get pull-up or pull-down, regardless of resistor value */
  400. S32 mt_get_gpio_pull_select_chip(u32 pin)
  401. {
  402. u32 reg, reg2;
  403. if (pin >= MAX_GPIO_PIN)
  404. return -ERINVAL;
  405. if ((PU_offset[pin].offset == (S8)-1) && (PUPD_offset[pin].offset == (S8)-1))
  406. return GPIO_PULL_UNSUPPORTED;
  407. if (PU_offset[pin].offset == (S8)-1) {
  408. reg = GPIO_RD32(PUPD_addr[pin].addr);
  409. /* Reg value: 0 for PU, 1 for PD --> reverse return value */
  410. return (S32)((reg & (1UL << PUPD_offset[pin].offset)) ? GPIO_PULL_DOWN : GPIO_PULL_UP);
  411. } else if (PUPD_offset[pin].offset == (S8)-1) {
  412. reg = (GPIO_RD32(PU_addr[pin].addr) >> PU_offset[pin].offset) & 1;
  413. reg2 = (GPIO_RD32(PD_addr[pin].addr) >> PD_offset[pin].offset) & 1;
  414. if (reg)
  415. return GPIO_PULL_UP;
  416. else if (reg2)
  417. return GPIO_PULL_DOWN;
  418. else
  419. return GPIO_NO_PULL;
  420. }
  421. return -ERINVAL;
  422. }
  423. /*---------------------------------------------------------------------------*/
  424. S32 mt_set_gpio_drv_chip(u32 pin, u32 drv)
  425. {
  426. u32 reg;
  427. u32 val, mask;
  428. struct mt_gpio_obj *obj = gpio_obj;
  429. if (!obj)
  430. return -ERACCESS;
  431. if (pin >= MAX_GPIO_PIN)
  432. return -ERINVAL;
  433. if (DRV_offset[pin].offset == (s8)-1)
  434. return GPIO_DRV_UNSUPPORTED;
  435. mask = ((1 << DRV_width[pin].width) - 1);
  436. /* check if set value beyond supported width */
  437. if (drv > mask)
  438. return -ERINVAL;
  439. reg = GPIO_RD32(DRV_addr[pin].addr);
  440. val = reg & ~(mask << DRV_offset[pin].offset);
  441. val |= (drv << DRV_offset[pin].offset);
  442. GPIO_WR32(DRV_addr[pin].addr, val);
  443. return RSUCCESS;
  444. }
  445. /*---------------------------------------------------------------------------*/
  446. S32 mt_get_gpio_drv_chip(u32 pin)
  447. {
  448. u32 reg;
  449. u32 val, mask;
  450. struct mt_gpio_obj *obj = gpio_obj;
  451. if (!obj)
  452. return -ERACCESS;
  453. if (pin >= MAX_GPIO_PIN)
  454. return -ERINVAL;
  455. if (DRV_offset[pin].offset == (s8)-1)
  456. return GPIO_DRV_UNSUPPORTED;
  457. reg = GPIO_RD32(DRV_addr[pin].addr);
  458. reg = reg >> DRV_offset[pin].offset;
  459. mask = (1 << DRV_width[pin].width) - 1;
  460. val = reg & mask;
  461. return val;
  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_dir(u32 pin, u32 dir)
  478. {
  479. mt_gpio_pin_decrypt(&pin);
  480. return mt_set_gpio_dir_chip(pin,dir);
  481. }
  482. /*---------------------------------------------------------------------------*/
  483. S32 mt_get_gpio_dir(u32 pin)
  484. {
  485. mt_gpio_pin_decrypt(&pin);
  486. return mt_get_gpio_dir_chip(pin);
  487. }
  488. /*---------------------------------------------------------------------------*/
  489. S32 mt_set_gpio_pull_enable(u32 pin, u32 enable)
  490. {
  491. mt_gpio_pin_decrypt(&pin);
  492. return mt_set_gpio_pull_enable_chip(pin,enable);
  493. }
  494. /*---------------------------------------------------------------------------*/
  495. S32 mt_get_gpio_pull_enable(u32 pin)
  496. {
  497. mt_gpio_pin_decrypt(&pin);
  498. return mt_get_gpio_pull_enable_chip(pin);
  499. }
  500. /*---------------------------------------------------------------------------*/
  501. S32 mt_set_gpio_pull_select(u32 pin, u32 select)
  502. {
  503. mt_gpio_pin_decrypt(&pin);
  504. return mt_set_gpio_pull_select_chip(pin,select);
  505. }
  506. /*---------------------------------------------------------------------------*/
  507. S32 mt_get_gpio_pull_select(u32 pin)
  508. {
  509. mt_gpio_pin_decrypt(&pin);
  510. return mt_get_gpio_pull_select_chip(pin);
  511. }
  512. /*---------------------------------------------------------------------------*/
  513. S32 mt_set_gpio_smt(u32 pin, u32 enable)
  514. {
  515. mt_gpio_pin_decrypt(&pin);
  516. return mt_set_gpio_smt_chip(pin,enable);
  517. }
  518. /*---------------------------------------------------------------------------*/
  519. S32 mt_get_gpio_smt(u32 pin)
  520. {
  521. mt_gpio_pin_decrypt(&pin);
  522. return mt_get_gpio_smt_chip(pin);
  523. }
  524. /*---------------------------------------------------------------------------*/
  525. S32 mt_set_gpio_ies(u32 pin, u32 enable)
  526. {
  527. mt_gpio_pin_decrypt(&pin);
  528. return mt_set_gpio_ies_chip(pin,enable);
  529. }
  530. /*---------------------------------------------------------------------------*/
  531. S32 mt_get_gpio_ies(u32 pin)
  532. {
  533. mt_gpio_pin_decrypt(&pin);
  534. return mt_get_gpio_ies_chip(pin);
  535. }
  536. /*---------------------------------------------------------------------------*/
  537. S32 mt_set_gpio_out(u32 pin, u32 output)
  538. {
  539. mt_gpio_pin_decrypt(&pin);
  540. return mt_set_gpio_out_chip(pin,output);
  541. }
  542. /*---------------------------------------------------------------------------*/
  543. S32 mt_get_gpio_out(u32 pin)
  544. {
  545. mt_gpio_pin_decrypt(&pin);
  546. return mt_get_gpio_out_chip(pin);
  547. }
  548. /*---------------------------------------------------------------------------*/
  549. S32 mt_get_gpio_in(u32 pin)
  550. {
  551. mt_gpio_pin_decrypt(&pin);
  552. return mt_get_gpio_in_chip(pin);
  553. }
  554. /*---------------------------------------------------------------------------*/
  555. S32 mt_set_gpio_mode(u32 pin, u32 mode)
  556. {
  557. mt_gpio_pin_decrypt(&pin);
  558. return mt_set_gpio_mode_chip(pin,mode);
  559. }
  560. /*---------------------------------------------------------------------------*/
  561. S32 mt_get_gpio_mode(u32 pin)
  562. {
  563. mt_gpio_pin_decrypt(&pin);
  564. return mt_get_gpio_mode_chip(pin);
  565. }
  566. /*---------------------------------------------------------------------------*/
  567. S32 mt_set_gpio_drv(u32 pin, u32 drv)
  568. {
  569. mt_gpio_pin_decrypt(&pin);
  570. return mt_set_gpio_drv_chip(pin,drv);
  571. }
  572. /*---------------------------------------------------------------------------*/
  573. S32 mt_get_gpio_drv(u32 pin)
  574. {
  575. mt_gpio_pin_decrypt(&pin);
  576. return mt_get_gpio_drv_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