gpio.c 18 KB

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