gpio.c 20 KB

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