gpio.c 20 KB

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