gpio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /*
  2. * ADI GPIO1 Abstraction Layer
  3. * Support BF50x, BF51x, BF52x, BF53x and BF561 only.
  4. *
  5. * Copyright 2006-2010 Analog Devices Inc.
  6. *
  7. * Licensed under the GPL-2 or later
  8. */
  9. #include <common.h>
  10. #include <linux/errno.h>
  11. #include <asm/gpio.h>
  12. #include <asm/portmux.h>
  13. #ifndef CONFIG_ADI_GPIO2
  14. #if ANOMALY_05000311 || ANOMALY_05000323
  15. enum {
  16. AWA_data = SYSCR,
  17. AWA_data_clear = SYSCR,
  18. AWA_data_set = SYSCR,
  19. AWA_toggle = SYSCR,
  20. AWA_maska = UART_SCR,
  21. AWA_maska_clear = UART_SCR,
  22. AWA_maska_set = UART_SCR,
  23. AWA_maska_toggle = UART_SCR,
  24. AWA_maskb = UART_GCTL,
  25. AWA_maskb_clear = UART_GCTL,
  26. AWA_maskb_set = UART_GCTL,
  27. AWA_maskb_toggle = UART_GCTL,
  28. AWA_dir = SPORT1_STAT,
  29. AWA_polar = SPORT1_STAT,
  30. AWA_edge = SPORT1_STAT,
  31. AWA_both = SPORT1_STAT,
  32. #if ANOMALY_05000311
  33. AWA_inen = TIMER_ENABLE,
  34. #elif ANOMALY_05000323
  35. AWA_inen = DMA1_1_CONFIG,
  36. #endif
  37. };
  38. /* Anomaly Workaround */
  39. #define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
  40. #else
  41. #define AWA_DUMMY_READ(...) do { } while (0)
  42. #endif
  43. static struct gpio_port_t * const gpio_array[] = {
  44. #if defined(BF533_FAMILY)
  45. (struct gpio_port_t *) FIO_FLAG_D,
  46. #elif defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x) \
  47. || defined(BF538_FAMILY) || defined(CONFIG_BF50x)
  48. (struct gpio_port_t *) PORTFIO,
  49. # if !defined(BF538_FAMILY)
  50. (struct gpio_port_t *) PORTGIO,
  51. (struct gpio_port_t *) PORTHIO,
  52. # endif
  53. #elif defined(BF561_FAMILY)
  54. (struct gpio_port_t *) FIO0_FLAG_D,
  55. (struct gpio_port_t *) FIO1_FLAG_D,
  56. (struct gpio_port_t *) FIO2_FLAG_D,
  57. #else
  58. # error no gpio arrays defined
  59. #endif
  60. };
  61. #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x) || \
  62. defined(CONFIG_BF50x)
  63. static unsigned short * const port_fer[] = {
  64. (unsigned short *) PORTF_FER,
  65. (unsigned short *) PORTG_FER,
  66. (unsigned short *) PORTH_FER,
  67. };
  68. # if !defined(BF537_FAMILY)
  69. static unsigned short * const port_mux[] = {
  70. (unsigned short *) PORTF_MUX,
  71. (unsigned short *) PORTG_MUX,
  72. (unsigned short *) PORTH_MUX,
  73. };
  74. static const
  75. u8 pmux_offset[][16] = {
  76. # if defined(CONFIG_BF52x)
  77. { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
  78. { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
  79. { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
  80. # elif defined(CONFIG_BF51x)
  81. { 0, 2, 2, 2, 2, 2, 2, 4, 6, 6, 6, 8, 8, 8, 8, 10 }, /* PORTF */
  82. { 0, 0, 0, 2, 4, 6, 6, 6, 8, 10, 10, 12, 14, 14, 14, 14 }, /* PORTG */
  83. { 0, 0, 0, 0, 2, 2, 4, 6, 10, 10, 10, 10, 10, 10, 10, 10 }, /* PORTH */
  84. # endif
  85. };
  86. # endif
  87. #elif defined(BF538_FAMILY)
  88. static unsigned short * const port_fer[] = {
  89. (unsigned short *) PORTCIO_FER,
  90. (unsigned short *) PORTDIO_FER,
  91. (unsigned short *) PORTEIO_FER,
  92. };
  93. #endif
  94. #ifdef CONFIG_BFIN_GPIO_TRACK
  95. #define RESOURCE_LABEL_SIZE 16
  96. static struct str_ident {
  97. char name[RESOURCE_LABEL_SIZE];
  98. } str_ident[MAX_RESOURCES];
  99. static void gpio_error(unsigned gpio)
  100. {
  101. printf("bfin-gpio: GPIO %d wasn't requested!\n", gpio);
  102. }
  103. static void set_label(unsigned short ident, const char *label)
  104. {
  105. if (label) {
  106. strncpy(str_ident[ident].name, label,
  107. RESOURCE_LABEL_SIZE);
  108. str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
  109. }
  110. }
  111. static char *get_label(unsigned short ident)
  112. {
  113. return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
  114. }
  115. static int cmp_label(unsigned short ident, const char *label)
  116. {
  117. if (label == NULL)
  118. printf("bfin-gpio: please provide none-null label\n");
  119. if (label)
  120. return strcmp(str_ident[ident].name, label);
  121. else
  122. return -EINVAL;
  123. }
  124. #define map_entry(m, i) reserved_##m##_map[gpio_bank(i)]
  125. #define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
  126. #define reserve(m, i) (map_entry(m, i) |= gpio_bit(i))
  127. #define unreserve(m, i) (map_entry(m, i) &= ~gpio_bit(i))
  128. #define DECLARE_RESERVED_MAP(m, c) static unsigned short reserved_##m##_map[c]
  129. #else
  130. #define is_reserved(m, i, e) (!(e))
  131. #define reserve(m, i)
  132. #define unreserve(m, i)
  133. #define DECLARE_RESERVED_MAP(m, c)
  134. #define gpio_error(gpio)
  135. #define set_label(...)
  136. #define get_label(...) ""
  137. #define cmp_label(...) 1
  138. #endif
  139. DECLARE_RESERVED_MAP(gpio, GPIO_BANK_NUM);
  140. DECLARE_RESERVED_MAP(peri, gpio_bank(MAX_RESOURCES));
  141. inline int check_gpio(unsigned gpio)
  142. {
  143. if (gpio >= MAX_BLACKFIN_GPIOS)
  144. return -EINVAL;
  145. return 0;
  146. }
  147. static void port_setup(unsigned gpio, unsigned short usage)
  148. {
  149. #if defined(BF538_FAMILY)
  150. /*
  151. * BF538/9 Port C,D and E are special.
  152. * Inverted PORT_FER polarity on CDE and no PORF_FER on F
  153. * Regular PORT F GPIOs are handled here, CDE are exclusively
  154. * managed by GPIOLIB
  155. */
  156. if (gpio < MAX_BLACKFIN_GPIOS || gpio >= MAX_RESOURCES)
  157. return;
  158. gpio -= MAX_BLACKFIN_GPIOS;
  159. if (usage == GPIO_USAGE)
  160. *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
  161. else
  162. *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
  163. SSYNC();
  164. return;
  165. #endif
  166. if (check_gpio(gpio))
  167. return;
  168. #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x) || \
  169. defined(CONFIG_BF50x)
  170. if (usage == GPIO_USAGE)
  171. *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
  172. else
  173. *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
  174. SSYNC();
  175. #endif
  176. }
  177. #ifdef BF537_FAMILY
  178. static struct {
  179. unsigned short res;
  180. unsigned short offset;
  181. } port_mux_lut[] = {
  182. {.res = P_PPI0_D13, .offset = 11},
  183. {.res = P_PPI0_D14, .offset = 11},
  184. {.res = P_PPI0_D15, .offset = 11},
  185. {.res = P_SPORT1_TFS, .offset = 11},
  186. {.res = P_SPORT1_TSCLK, .offset = 11},
  187. {.res = P_SPORT1_DTPRI, .offset = 11},
  188. {.res = P_PPI0_D10, .offset = 10},
  189. {.res = P_PPI0_D11, .offset = 10},
  190. {.res = P_PPI0_D12, .offset = 10},
  191. {.res = P_SPORT1_RSCLK, .offset = 10},
  192. {.res = P_SPORT1_RFS, .offset = 10},
  193. {.res = P_SPORT1_DRPRI, .offset = 10},
  194. {.res = P_PPI0_D8, .offset = 9},
  195. {.res = P_PPI0_D9, .offset = 9},
  196. {.res = P_SPORT1_DRSEC, .offset = 9},
  197. {.res = P_SPORT1_DTSEC, .offset = 9},
  198. {.res = P_TMR2, .offset = 8},
  199. {.res = P_PPI0_FS3, .offset = 8},
  200. {.res = P_TMR3, .offset = 7},
  201. {.res = P_SPI0_SSEL4, .offset = 7},
  202. {.res = P_TMR4, .offset = 6},
  203. {.res = P_SPI0_SSEL5, .offset = 6},
  204. {.res = P_TMR5, .offset = 5},
  205. {.res = P_SPI0_SSEL6, .offset = 5},
  206. {.res = P_UART1_RX, .offset = 4},
  207. {.res = P_UART1_TX, .offset = 4},
  208. {.res = P_TMR6, .offset = 4},
  209. {.res = P_TMR7, .offset = 4},
  210. {.res = P_UART0_RX, .offset = 3},
  211. {.res = P_UART0_TX, .offset = 3},
  212. {.res = P_DMAR0, .offset = 3},
  213. {.res = P_DMAR1, .offset = 3},
  214. {.res = P_SPORT0_DTSEC, .offset = 1},
  215. {.res = P_SPORT0_DRSEC, .offset = 1},
  216. {.res = P_CAN0_RX, .offset = 1},
  217. {.res = P_CAN0_TX, .offset = 1},
  218. {.res = P_SPI0_SSEL7, .offset = 1},
  219. {.res = P_SPORT0_TFS, .offset = 0},
  220. {.res = P_SPORT0_DTPRI, .offset = 0},
  221. {.res = P_SPI0_SSEL2, .offset = 0},
  222. {.res = P_SPI0_SSEL3, .offset = 0},
  223. };
  224. static void portmux_setup(unsigned short per)
  225. {
  226. u16 y, offset, muxreg, mask;
  227. u16 function = P_FUNCT2MUX(per);
  228. for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
  229. if (port_mux_lut[y].res == per) {
  230. /* SET PORTMUX REG */
  231. offset = port_mux_lut[y].offset;
  232. muxreg = bfin_read_PORT_MUX();
  233. if (offset == 1)
  234. mask = 3;
  235. else
  236. mask = 1;
  237. muxreg &= ~(mask << offset);
  238. muxreg |= ((function & mask) << offset);
  239. bfin_write_PORT_MUX(muxreg);
  240. }
  241. }
  242. }
  243. #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
  244. inline void portmux_setup(unsigned short per)
  245. {
  246. u16 pmux, ident = P_IDENT(per), function = P_FUNCT2MUX(per);
  247. u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
  248. pmux = *port_mux[gpio_bank(ident)];
  249. pmux &= ~(3 << offset);
  250. pmux |= (function & 3) << offset;
  251. *port_mux[gpio_bank(ident)] = pmux;
  252. SSYNC();
  253. }
  254. #else
  255. # define portmux_setup(...) do { } while (0)
  256. #endif
  257. /***********************************************************
  258. *
  259. * FUNCTIONS: Blackfin General Purpose Ports Access Functions
  260. *
  261. * INPUTS/OUTPUTS:
  262. * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
  263. *
  264. *
  265. * DESCRIPTION: These functions abstract direct register access
  266. * to Blackfin processor General Purpose
  267. * Ports Regsiters
  268. *
  269. * CAUTION: These functions do not belong to the GPIO Driver API
  270. *************************************************************
  271. * MODIFICATION HISTORY :
  272. **************************************************************/
  273. /* Set a specific bit */
  274. #define SET_GPIO(name) \
  275. void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
  276. { \
  277. unsigned long flags; \
  278. local_irq_save(flags); \
  279. if (arg) \
  280. gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
  281. else \
  282. gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
  283. AWA_DUMMY_READ(name); \
  284. local_irq_restore(flags); \
  285. }
  286. SET_GPIO(dir) /* set_gpio_dir() */
  287. SET_GPIO(inen) /* set_gpio_inen() */
  288. SET_GPIO(polar) /* set_gpio_polar() */
  289. SET_GPIO(edge) /* set_gpio_edge() */
  290. SET_GPIO(both) /* set_gpio_both() */
  291. #define SET_GPIO_SC(name) \
  292. void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
  293. { \
  294. unsigned long flags; \
  295. if (ANOMALY_05000311 || ANOMALY_05000323) \
  296. local_irq_save(flags); \
  297. if (arg) \
  298. gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
  299. else \
  300. gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
  301. if (ANOMALY_05000311 || ANOMALY_05000323) { \
  302. AWA_DUMMY_READ(name); \
  303. local_irq_restore(flags); \
  304. } \
  305. }
  306. SET_GPIO_SC(maska)
  307. SET_GPIO_SC(maskb)
  308. SET_GPIO_SC(data)
  309. void set_gpio_toggle(unsigned gpio)
  310. {
  311. unsigned long flags;
  312. if (ANOMALY_05000311 || ANOMALY_05000323)
  313. local_irq_save(flags);
  314. gpio_array[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
  315. if (ANOMALY_05000311 || ANOMALY_05000323) {
  316. AWA_DUMMY_READ(toggle);
  317. local_irq_restore(flags);
  318. }
  319. }
  320. /* Set current PORT date (16-bit word) */
  321. #define SET_GPIO_P(name) \
  322. void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
  323. { \
  324. unsigned long flags; \
  325. if (ANOMALY_05000311 || ANOMALY_05000323) \
  326. local_irq_save(flags); \
  327. gpio_array[gpio_bank(gpio)]->name = arg; \
  328. if (ANOMALY_05000311 || ANOMALY_05000323) { \
  329. AWA_DUMMY_READ(name); \
  330. local_irq_restore(flags); \
  331. } \
  332. }
  333. SET_GPIO_P(data)
  334. SET_GPIO_P(dir)
  335. SET_GPIO_P(inen)
  336. SET_GPIO_P(polar)
  337. SET_GPIO_P(edge)
  338. SET_GPIO_P(both)
  339. SET_GPIO_P(maska)
  340. SET_GPIO_P(maskb)
  341. /* Get a specific bit */
  342. #define GET_GPIO(name) \
  343. unsigned short get_gpio_ ## name(unsigned gpio) \
  344. { \
  345. unsigned long flags; \
  346. unsigned short ret; \
  347. if (ANOMALY_05000311 || ANOMALY_05000323) \
  348. local_irq_save(flags); \
  349. ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
  350. if (ANOMALY_05000311 || ANOMALY_05000323) { \
  351. AWA_DUMMY_READ(name); \
  352. local_irq_restore(flags); \
  353. } \
  354. return ret; \
  355. }
  356. GET_GPIO(data)
  357. GET_GPIO(dir)
  358. GET_GPIO(inen)
  359. GET_GPIO(polar)
  360. GET_GPIO(edge)
  361. GET_GPIO(both)
  362. GET_GPIO(maska)
  363. GET_GPIO(maskb)
  364. /* Get current PORT date (16-bit word) */
  365. #define GET_GPIO_P(name) \
  366. unsigned short get_gpiop_ ## name(unsigned gpio) \
  367. { \
  368. unsigned long flags; \
  369. unsigned short ret; \
  370. if (ANOMALY_05000311 || ANOMALY_05000323) \
  371. local_irq_save(flags); \
  372. ret = (gpio_array[gpio_bank(gpio)]->name); \
  373. if (ANOMALY_05000311 || ANOMALY_05000323) { \
  374. AWA_DUMMY_READ(name); \
  375. local_irq_restore(flags); \
  376. } \
  377. return ret; \
  378. }
  379. GET_GPIO_P(data)
  380. GET_GPIO_P(dir)
  381. GET_GPIO_P(inen)
  382. GET_GPIO_P(polar)
  383. GET_GPIO_P(edge)
  384. GET_GPIO_P(both)
  385. GET_GPIO_P(maska)
  386. GET_GPIO_P(maskb)
  387. /***********************************************************
  388. *
  389. * FUNCTIONS: Blackfin Peripheral Resource Allocation
  390. * and PortMux Setup
  391. *
  392. * INPUTS/OUTPUTS:
  393. * per Peripheral Identifier
  394. * label String
  395. *
  396. * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
  397. *
  398. * CAUTION:
  399. *************************************************************
  400. * MODIFICATION HISTORY :
  401. **************************************************************/
  402. int peripheral_request(unsigned short per, const char *label)
  403. {
  404. unsigned short ident = P_IDENT(per);
  405. /*
  406. * Don't cares are pins with only one dedicated function
  407. */
  408. if (per & P_DONTCARE)
  409. return 0;
  410. if (!(per & P_DEFINED))
  411. return -ENODEV;
  412. BUG_ON(ident >= MAX_RESOURCES);
  413. /* If a pin can be muxed as either GPIO or peripheral, make
  414. * sure it is not already a GPIO pin when we request it.
  415. */
  416. if (unlikely(!check_gpio(ident) && is_reserved(gpio, ident, 1))) {
  417. printf("%s: Peripheral %d is already reserved as GPIO by %s !\n",
  418. __func__, ident, get_label(ident));
  419. return -EBUSY;
  420. }
  421. if (unlikely(is_reserved(peri, ident, 1))) {
  422. /*
  423. * Pin functions like AMC address strobes my
  424. * be requested and used by several drivers
  425. */
  426. if (!(per & P_MAYSHARE)) {
  427. /*
  428. * Allow that the identical pin function can
  429. * be requested from the same driver twice
  430. */
  431. if (cmp_label(ident, label) == 0)
  432. goto anyway;
  433. printf("%s: Peripheral %d function %d is already reserved by %s !\n",
  434. __func__, ident, P_FUNCT2MUX(per), get_label(ident));
  435. return -EBUSY;
  436. }
  437. }
  438. anyway:
  439. reserve(peri, ident);
  440. portmux_setup(per);
  441. port_setup(ident, PERIPHERAL_USAGE);
  442. set_label(ident, label);
  443. return 0;
  444. }
  445. int peripheral_request_list(const unsigned short per[], const char *label)
  446. {
  447. u16 cnt;
  448. int ret;
  449. for (cnt = 0; per[cnt] != 0; cnt++) {
  450. ret = peripheral_request(per[cnt], label);
  451. if (ret < 0) {
  452. for ( ; cnt > 0; cnt--)
  453. peripheral_free(per[cnt - 1]);
  454. return ret;
  455. }
  456. }
  457. return 0;
  458. }
  459. void peripheral_free(unsigned short per)
  460. {
  461. unsigned short ident = P_IDENT(per);
  462. if (per & P_DONTCARE)
  463. return;
  464. if (!(per & P_DEFINED))
  465. return;
  466. if (unlikely(!is_reserved(peri, ident, 0)))
  467. return;
  468. if (!(per & P_MAYSHARE))
  469. port_setup(ident, GPIO_USAGE);
  470. unreserve(peri, ident);
  471. set_label(ident, "free");
  472. }
  473. void peripheral_free_list(const unsigned short per[])
  474. {
  475. u16 cnt;
  476. for (cnt = 0; per[cnt] != 0; cnt++)
  477. peripheral_free(per[cnt]);
  478. }
  479. /***********************************************************
  480. *
  481. * FUNCTIONS: Blackfin GPIO Driver
  482. *
  483. * INPUTS/OUTPUTS:
  484. * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
  485. * label String
  486. *
  487. * DESCRIPTION: Blackfin GPIO Driver API
  488. *
  489. * CAUTION:
  490. *************************************************************
  491. * MODIFICATION HISTORY :
  492. **************************************************************/
  493. int gpio_request(unsigned gpio, const char *label)
  494. {
  495. if (check_gpio(gpio) < 0)
  496. return -EINVAL;
  497. /*
  498. * Allow that the identical GPIO can
  499. * be requested from the same driver twice
  500. * Do nothing and return -
  501. */
  502. if (cmp_label(gpio, label) == 0)
  503. return 0;
  504. if (unlikely(is_reserved(gpio, gpio, 1))) {
  505. printf("bfin-gpio: GPIO %d is already reserved by %s !\n",
  506. gpio, get_label(gpio));
  507. return -EBUSY;
  508. }
  509. if (unlikely(is_reserved(peri, gpio, 1))) {
  510. printf("bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
  511. gpio, get_label(gpio));
  512. return -EBUSY;
  513. }
  514. else { /* Reset POLAR setting when acquiring a gpio for the first time */
  515. set_gpio_polar(gpio, 0);
  516. }
  517. reserve(gpio, gpio);
  518. set_label(gpio, label);
  519. port_setup(gpio, GPIO_USAGE);
  520. return 0;
  521. }
  522. int gpio_free(unsigned gpio)
  523. {
  524. if (check_gpio(gpio) < 0)
  525. return -1;
  526. if (unlikely(!is_reserved(gpio, gpio, 0))) {
  527. gpio_error(gpio);
  528. return -1;
  529. }
  530. unreserve(gpio, gpio);
  531. set_label(gpio, "free");
  532. return 0;
  533. }
  534. #ifdef ADI_SPECIAL_GPIO_BANKS
  535. DECLARE_RESERVED_MAP(special_gpio, gpio_bank(MAX_RESOURCES));
  536. int special_gpio_request(unsigned gpio, const char *label)
  537. {
  538. /*
  539. * Allow that the identical GPIO can
  540. * be requested from the same driver twice
  541. * Do nothing and return -
  542. */
  543. if (cmp_label(gpio, label) == 0)
  544. return 0;
  545. if (unlikely(is_reserved(special_gpio, gpio, 1))) {
  546. printf("bfin-gpio: GPIO %d is already reserved by %s !\n",
  547. gpio, get_label(gpio));
  548. return -EBUSY;
  549. }
  550. if (unlikely(is_reserved(peri, gpio, 1))) {
  551. printf("bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
  552. gpio, get_label(gpio));
  553. return -EBUSY;
  554. }
  555. reserve(special_gpio, gpio);
  556. reserve(peri, gpio);
  557. set_label(gpio, label);
  558. port_setup(gpio, GPIO_USAGE);
  559. return 0;
  560. }
  561. void special_gpio_free(unsigned gpio)
  562. {
  563. if (unlikely(!is_reserved(special_gpio, gpio, 0))) {
  564. gpio_error(gpio);
  565. return;
  566. }
  567. unreserve(special_gpio, gpio);
  568. unreserve(peri, gpio);
  569. set_label(gpio, "free");
  570. }
  571. #endif
  572. static inline void __gpio_direction_input(unsigned gpio)
  573. {
  574. gpio_array[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
  575. gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
  576. }
  577. int gpio_direction_input(unsigned gpio)
  578. {
  579. unsigned long flags;
  580. if (!is_reserved(gpio, gpio, 0)) {
  581. gpio_error(gpio);
  582. return -EINVAL;
  583. }
  584. local_irq_save(flags);
  585. __gpio_direction_input(gpio);
  586. AWA_DUMMY_READ(inen);
  587. local_irq_restore(flags);
  588. return 0;
  589. }
  590. int gpio_set_value(unsigned gpio, int arg)
  591. {
  592. if (arg)
  593. gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
  594. else
  595. gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
  596. return 0;
  597. }
  598. int gpio_direction_output(unsigned gpio, int value)
  599. {
  600. unsigned long flags;
  601. if (!is_reserved(gpio, gpio, 0)) {
  602. gpio_error(gpio);
  603. return -EINVAL;
  604. }
  605. local_irq_save(flags);
  606. gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
  607. gpio_set_value(gpio, value);
  608. gpio_array[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
  609. AWA_DUMMY_READ(dir);
  610. local_irq_restore(flags);
  611. return 0;
  612. }
  613. int gpio_get_value(unsigned gpio)
  614. {
  615. unsigned long flags;
  616. if (unlikely(get_gpio_edge(gpio))) {
  617. int ret;
  618. local_irq_save(flags);
  619. set_gpio_edge(gpio, 0);
  620. ret = get_gpio_data(gpio);
  621. set_gpio_edge(gpio, 1);
  622. local_irq_restore(flags);
  623. return ret;
  624. } else
  625. return get_gpio_data(gpio);
  626. }
  627. /* If we are booting from SPI and our board lacks a strong enough pull up,
  628. * the core can reset and execute the bootrom faster than the resistor can
  629. * pull the signal logically high. To work around this (common) error in
  630. * board design, we explicitly set the pin back to GPIO mode, force /CS
  631. * high, and wait for the electrons to do their thing.
  632. *
  633. * This function only makes sense to be called from reset code, but it
  634. * lives here as we need to force all the GPIO states w/out going through
  635. * BUG() checks and such.
  636. */
  637. void bfin_reset_boot_spi_cs(unsigned short pin)
  638. {
  639. unsigned short gpio = P_IDENT(pin);
  640. port_setup(gpio, GPIO_USAGE);
  641. gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
  642. AWA_DUMMY_READ(data_set);
  643. udelay(1);
  644. }
  645. int name_to_gpio(const char *name)
  646. {
  647. int port_base;
  648. if (tolower(*name) == 'p') {
  649. ++name;
  650. switch (tolower(*name)) {
  651. #ifdef GPIO_PA0
  652. case 'a': port_base = GPIO_PA0; break;
  653. #endif
  654. #ifdef GPIO_PB0
  655. case 'b': port_base = GPIO_PB0; break;
  656. #endif
  657. #ifdef GPIO_PC0
  658. case 'c': port_base = GPIO_PC0; break;
  659. #endif
  660. #ifdef GPIO_PD0
  661. case 'd': port_base = GPIO_PD0; break;
  662. #endif
  663. #ifdef GPIO_PE0
  664. case 'e': port_base = GPIO_PE0; break;
  665. #endif
  666. #ifdef GPIO_PF0
  667. case 'f': port_base = GPIO_PF0; break;
  668. #endif
  669. #ifdef GPIO_PG0
  670. case 'g': port_base = GPIO_PG0; break;
  671. #endif
  672. #ifdef GPIO_PH0
  673. case 'h': port_base = GPIO_PH0; break;
  674. #endif
  675. #ifdef GPIO_PI0
  676. case 'i': port_base = GPIO_PI0; break;
  677. #endif
  678. #ifdef GPIO_PJ
  679. case 'j': port_base = GPIO_PJ0; break;
  680. #endif
  681. default: return -1;
  682. }
  683. ++name;
  684. } else
  685. port_base = 0;
  686. return port_base + simple_strtoul(name, NULL, 10);
  687. }
  688. void gpio_labels(void)
  689. {
  690. int c, gpio;
  691. for (c = 0; c < MAX_RESOURCES; c++) {
  692. gpio = is_reserved(gpio, c, 1);
  693. if (!check_gpio(c) && gpio)
  694. printf("GPIO_%d:\t%s\tGPIO %s\n", c,
  695. get_label(c),
  696. get_gpio_dir(c) ? "OUTPUT" : "INPUT");
  697. else if (is_reserved(peri, c, 1))
  698. printf("GPIO_%d:\t%s\tPeripheral\n", c, get_label(c));
  699. else
  700. continue;
  701. }
  702. }
  703. #else
  704. struct gpio_port_t * const gpio_array[] = {
  705. (struct gpio_port_t *)PORTA_FER,
  706. (struct gpio_port_t *)PORTB_FER,
  707. (struct gpio_port_t *)PORTC_FER,
  708. (struct gpio_port_t *)PORTD_FER,
  709. (struct gpio_port_t *)PORTE_FER,
  710. (struct gpio_port_t *)PORTF_FER,
  711. (struct gpio_port_t *)PORTG_FER,
  712. #if defined(CONFIG_BF54x)
  713. (struct gpio_port_t *)PORTH_FER,
  714. (struct gpio_port_t *)PORTI_FER,
  715. (struct gpio_port_t *)PORTJ_FER,
  716. #endif
  717. };
  718. #endif