gpio.c 21 KB

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