bfin_gpio.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. /*
  2. * GPIO Abstraction Layer
  3. *
  4. * Copyright 2006-2009 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/module.h>
  10. #include <linux/err.h>
  11. #include <linux/proc_fs.h>
  12. #include <asm/blackfin.h>
  13. #include <asm/gpio.h>
  14. #include <asm/portmux.h>
  15. #include <linux/irq.h>
  16. #if ANOMALY_05000311 || ANOMALY_05000323
  17. enum {
  18. AWA_data = SYSCR,
  19. AWA_data_clear = SYSCR,
  20. AWA_data_set = SYSCR,
  21. AWA_toggle = SYSCR,
  22. AWA_maska = BFIN_UART_SCR,
  23. AWA_maska_clear = BFIN_UART_SCR,
  24. AWA_maska_set = BFIN_UART_SCR,
  25. AWA_maska_toggle = BFIN_UART_SCR,
  26. AWA_maskb = BFIN_UART_GCTL,
  27. AWA_maskb_clear = BFIN_UART_GCTL,
  28. AWA_maskb_set = BFIN_UART_GCTL,
  29. AWA_maskb_toggle = BFIN_UART_GCTL,
  30. AWA_dir = SPORT1_STAT,
  31. AWA_polar = SPORT1_STAT,
  32. AWA_edge = SPORT1_STAT,
  33. AWA_both = SPORT1_STAT,
  34. #if ANOMALY_05000311
  35. AWA_inen = TIMER_ENABLE,
  36. #elif ANOMALY_05000323
  37. AWA_inen = DMA1_1_CONFIG,
  38. #endif
  39. };
  40. /* Anomaly Workaround */
  41. #define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
  42. #else
  43. #define AWA_DUMMY_READ(...) do { } while (0)
  44. #endif
  45. static struct gpio_port_t * const gpio_array[] = {
  46. #if defined(BF533_FAMILY) || defined(BF538_FAMILY)
  47. (struct gpio_port_t *) FIO_FLAG_D,
  48. #elif defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
  49. (struct gpio_port_t *) PORTFIO,
  50. (struct gpio_port_t *) PORTGIO,
  51. (struct gpio_port_t *) PORTHIO,
  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. #elif defined(CONFIG_BF54x)
  57. (struct gpio_port_t *)PORTA_FER,
  58. (struct gpio_port_t *)PORTB_FER,
  59. (struct gpio_port_t *)PORTC_FER,
  60. (struct gpio_port_t *)PORTD_FER,
  61. (struct gpio_port_t *)PORTE_FER,
  62. (struct gpio_port_t *)PORTF_FER,
  63. (struct gpio_port_t *)PORTG_FER,
  64. (struct gpio_port_t *)PORTH_FER,
  65. (struct gpio_port_t *)PORTI_FER,
  66. (struct gpio_port_t *)PORTJ_FER,
  67. #else
  68. # error no gpio arrays defined
  69. #endif
  70. };
  71. #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
  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. static unsigned short reserved_gpio_map[GPIO_BANK_NUM];
  104. static unsigned short reserved_peri_map[gpio_bank(MAX_RESOURCES)];
  105. static unsigned short reserved_gpio_irq_map[GPIO_BANK_NUM];
  106. #define RESOURCE_LABEL_SIZE 16
  107. static struct str_ident {
  108. char name[RESOURCE_LABEL_SIZE];
  109. } str_ident[MAX_RESOURCES];
  110. #if defined(CONFIG_PM)
  111. static struct gpio_port_s gpio_bank_saved[GPIO_BANK_NUM];
  112. #endif
  113. inline int check_gpio(unsigned gpio)
  114. {
  115. #if defined(CONFIG_BF54x)
  116. if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15
  117. || gpio == GPIO_PH14 || gpio == GPIO_PH15
  118. || gpio == GPIO_PJ14 || gpio == GPIO_PJ15)
  119. return -EINVAL;
  120. #endif
  121. if (gpio >= MAX_BLACKFIN_GPIOS)
  122. return -EINVAL;
  123. return 0;
  124. }
  125. static void gpio_error(unsigned gpio)
  126. {
  127. printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
  128. }
  129. static void set_label(unsigned short ident, const char *label)
  130. {
  131. if (label) {
  132. strncpy(str_ident[ident].name, label,
  133. RESOURCE_LABEL_SIZE);
  134. str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
  135. }
  136. }
  137. static char *get_label(unsigned short ident)
  138. {
  139. return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
  140. }
  141. static int cmp_label(unsigned short ident, const char *label)
  142. {
  143. if (label == NULL) {
  144. dump_stack();
  145. printk(KERN_ERR "Please provide none-null label\n");
  146. }
  147. if (label)
  148. return strcmp(str_ident[ident].name, label);
  149. else
  150. return -EINVAL;
  151. }
  152. static void port_setup(unsigned gpio, unsigned short usage)
  153. {
  154. #if defined(BF538_FAMILY)
  155. /*
  156. * BF538/9 Port C,D and E are special.
  157. * Inverted PORT_FER polarity on CDE and no PORF_FER on F
  158. * Regular PORT F GPIOs are handled here, CDE are exclusively
  159. * managed by GPIOLIB
  160. */
  161. if (gpio < MAX_BLACKFIN_GPIOS || gpio >= MAX_RESOURCES)
  162. return;
  163. gpio -= MAX_BLACKFIN_GPIOS;
  164. if (usage == GPIO_USAGE)
  165. *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
  166. else
  167. *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
  168. SSYNC();
  169. return;
  170. #endif
  171. if (check_gpio(gpio))
  172. return;
  173. #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
  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. #elif defined(CONFIG_BF54x)
  180. if (usage == GPIO_USAGE)
  181. gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
  182. else
  183. gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
  184. SSYNC();
  185. #endif
  186. }
  187. #ifdef BF537_FAMILY
  188. static struct {
  189. unsigned short res;
  190. unsigned short offset;
  191. } port_mux_lut[] = {
  192. {.res = P_PPI0_D13, .offset = 11},
  193. {.res = P_PPI0_D14, .offset = 11},
  194. {.res = P_PPI0_D15, .offset = 11},
  195. {.res = P_SPORT1_TFS, .offset = 11},
  196. {.res = P_SPORT1_TSCLK, .offset = 11},
  197. {.res = P_SPORT1_DTPRI, .offset = 11},
  198. {.res = P_PPI0_D10, .offset = 10},
  199. {.res = P_PPI0_D11, .offset = 10},
  200. {.res = P_PPI0_D12, .offset = 10},
  201. {.res = P_SPORT1_RSCLK, .offset = 10},
  202. {.res = P_SPORT1_RFS, .offset = 10},
  203. {.res = P_SPORT1_DRPRI, .offset = 10},
  204. {.res = P_PPI0_D8, .offset = 9},
  205. {.res = P_PPI0_D9, .offset = 9},
  206. {.res = P_SPORT1_DRSEC, .offset = 9},
  207. {.res = P_SPORT1_DTSEC, .offset = 9},
  208. {.res = P_TMR2, .offset = 8},
  209. {.res = P_PPI0_FS3, .offset = 8},
  210. {.res = P_TMR3, .offset = 7},
  211. {.res = P_SPI0_SSEL4, .offset = 7},
  212. {.res = P_TMR4, .offset = 6},
  213. {.res = P_SPI0_SSEL5, .offset = 6},
  214. {.res = P_TMR5, .offset = 5},
  215. {.res = P_SPI0_SSEL6, .offset = 5},
  216. {.res = P_UART1_RX, .offset = 4},
  217. {.res = P_UART1_TX, .offset = 4},
  218. {.res = P_TMR6, .offset = 4},
  219. {.res = P_TMR7, .offset = 4},
  220. {.res = P_UART0_RX, .offset = 3},
  221. {.res = P_UART0_TX, .offset = 3},
  222. {.res = P_DMAR0, .offset = 3},
  223. {.res = P_DMAR1, .offset = 3},
  224. {.res = P_SPORT0_DTSEC, .offset = 1},
  225. {.res = P_SPORT0_DRSEC, .offset = 1},
  226. {.res = P_CAN0_RX, .offset = 1},
  227. {.res = P_CAN0_TX, .offset = 1},
  228. {.res = P_SPI0_SSEL7, .offset = 1},
  229. {.res = P_SPORT0_TFS, .offset = 0},
  230. {.res = P_SPORT0_DTPRI, .offset = 0},
  231. {.res = P_SPI0_SSEL2, .offset = 0},
  232. {.res = P_SPI0_SSEL3, .offset = 0},
  233. };
  234. static void portmux_setup(unsigned short per)
  235. {
  236. u16 y, offset, muxreg;
  237. u16 function = P_FUNCT2MUX(per);
  238. for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
  239. if (port_mux_lut[y].res == per) {
  240. /* SET PORTMUX REG */
  241. offset = port_mux_lut[y].offset;
  242. muxreg = bfin_read_PORT_MUX();
  243. if (offset != 1)
  244. muxreg &= ~(1 << offset);
  245. else
  246. muxreg &= ~(3 << 1);
  247. muxreg |= (function << offset);
  248. bfin_write_PORT_MUX(muxreg);
  249. }
  250. }
  251. }
  252. #elif defined(CONFIG_BF54x)
  253. inline void portmux_setup(unsigned short per)
  254. {
  255. u32 pmux;
  256. u16 ident = P_IDENT(per);
  257. u16 function = P_FUNCT2MUX(per);
  258. pmux = gpio_array[gpio_bank(ident)]->port_mux;
  259. pmux &= ~(0x3 << (2 * gpio_sub_n(ident)));
  260. pmux |= (function & 0x3) << (2 * gpio_sub_n(ident));
  261. gpio_array[gpio_bank(ident)]->port_mux = pmux;
  262. }
  263. inline u16 get_portmux(unsigned short per)
  264. {
  265. u32 pmux;
  266. u16 ident = P_IDENT(per);
  267. pmux = gpio_array[gpio_bank(ident)]->port_mux;
  268. return (pmux >> (2 * gpio_sub_n(ident)) & 0x3);
  269. }
  270. #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
  271. inline void portmux_setup(unsigned short per)
  272. {
  273. u16 pmux, ident = P_IDENT(per), function = P_FUNCT2MUX(per);
  274. u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
  275. pmux = *port_mux[gpio_bank(ident)];
  276. pmux &= ~(3 << offset);
  277. pmux |= (function & 3) << offset;
  278. *port_mux[gpio_bank(ident)] = pmux;
  279. SSYNC();
  280. }
  281. #else
  282. # define portmux_setup(...) do { } while (0)
  283. #endif
  284. #ifndef CONFIG_BF54x
  285. /***********************************************************
  286. *
  287. * FUNCTIONS: Blackfin General Purpose Ports Access Functions
  288. *
  289. * INPUTS/OUTPUTS:
  290. * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
  291. *
  292. *
  293. * DESCRIPTION: These functions abstract direct register access
  294. * to Blackfin processor General Purpose
  295. * Ports Regsiters
  296. *
  297. * CAUTION: These functions do not belong to the GPIO Driver API
  298. *************************************************************
  299. * MODIFICATION HISTORY :
  300. **************************************************************/
  301. /* Set a specific bit */
  302. #define SET_GPIO(name) \
  303. void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
  304. { \
  305. unsigned long flags; \
  306. local_irq_save_hw(flags); \
  307. if (arg) \
  308. gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
  309. else \
  310. gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
  311. AWA_DUMMY_READ(name); \
  312. local_irq_restore_hw(flags); \
  313. } \
  314. EXPORT_SYMBOL(set_gpio_ ## name);
  315. SET_GPIO(dir) /* set_gpio_dir() */
  316. SET_GPIO(inen) /* set_gpio_inen() */
  317. SET_GPIO(polar) /* set_gpio_polar() */
  318. SET_GPIO(edge) /* set_gpio_edge() */
  319. SET_GPIO(both) /* set_gpio_both() */
  320. #define SET_GPIO_SC(name) \
  321. void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
  322. { \
  323. unsigned long flags; \
  324. if (ANOMALY_05000311 || ANOMALY_05000323) \
  325. local_irq_save_hw(flags); \
  326. if (arg) \
  327. gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
  328. else \
  329. gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
  330. if (ANOMALY_05000311 || ANOMALY_05000323) { \
  331. AWA_DUMMY_READ(name); \
  332. local_irq_restore_hw(flags); \
  333. } \
  334. } \
  335. EXPORT_SYMBOL(set_gpio_ ## name);
  336. SET_GPIO_SC(maska)
  337. SET_GPIO_SC(maskb)
  338. SET_GPIO_SC(data)
  339. void set_gpio_toggle(unsigned gpio)
  340. {
  341. unsigned long flags;
  342. if (ANOMALY_05000311 || ANOMALY_05000323)
  343. local_irq_save_hw(flags);
  344. gpio_array[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
  345. if (ANOMALY_05000311 || ANOMALY_05000323) {
  346. AWA_DUMMY_READ(toggle);
  347. local_irq_restore_hw(flags);
  348. }
  349. }
  350. EXPORT_SYMBOL(set_gpio_toggle);
  351. /*Set current PORT date (16-bit word)*/
  352. #define SET_GPIO_P(name) \
  353. void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
  354. { \
  355. unsigned long flags; \
  356. if (ANOMALY_05000311 || ANOMALY_05000323) \
  357. local_irq_save_hw(flags); \
  358. gpio_array[gpio_bank(gpio)]->name = arg; \
  359. if (ANOMALY_05000311 || ANOMALY_05000323) { \
  360. AWA_DUMMY_READ(name); \
  361. local_irq_restore_hw(flags); \
  362. } \
  363. } \
  364. EXPORT_SYMBOL(set_gpiop_ ## name);
  365. SET_GPIO_P(data)
  366. SET_GPIO_P(dir)
  367. SET_GPIO_P(inen)
  368. SET_GPIO_P(polar)
  369. SET_GPIO_P(edge)
  370. SET_GPIO_P(both)
  371. SET_GPIO_P(maska)
  372. SET_GPIO_P(maskb)
  373. /* Get a specific bit */
  374. #define GET_GPIO(name) \
  375. unsigned short get_gpio_ ## name(unsigned gpio) \
  376. { \
  377. unsigned long flags; \
  378. unsigned short ret; \
  379. if (ANOMALY_05000311 || ANOMALY_05000323) \
  380. local_irq_save_hw(flags); \
  381. ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
  382. if (ANOMALY_05000311 || ANOMALY_05000323) { \
  383. AWA_DUMMY_READ(name); \
  384. local_irq_restore_hw(flags); \
  385. } \
  386. return ret; \
  387. } \
  388. EXPORT_SYMBOL(get_gpio_ ## name);
  389. GET_GPIO(data)
  390. GET_GPIO(dir)
  391. GET_GPIO(inen)
  392. GET_GPIO(polar)
  393. GET_GPIO(edge)
  394. GET_GPIO(both)
  395. GET_GPIO(maska)
  396. GET_GPIO(maskb)
  397. /*Get current PORT date (16-bit word)*/
  398. #define GET_GPIO_P(name) \
  399. unsigned short get_gpiop_ ## name(unsigned gpio) \
  400. { \
  401. unsigned long flags; \
  402. unsigned short ret; \
  403. if (ANOMALY_05000311 || ANOMALY_05000323) \
  404. local_irq_save_hw(flags); \
  405. ret = (gpio_array[gpio_bank(gpio)]->name); \
  406. if (ANOMALY_05000311 || ANOMALY_05000323) { \
  407. AWA_DUMMY_READ(name); \
  408. local_irq_restore_hw(flags); \
  409. } \
  410. return ret; \
  411. } \
  412. EXPORT_SYMBOL(get_gpiop_ ## name);
  413. GET_GPIO_P(data)
  414. GET_GPIO_P(dir)
  415. GET_GPIO_P(inen)
  416. GET_GPIO_P(polar)
  417. GET_GPIO_P(edge)
  418. GET_GPIO_P(both)
  419. GET_GPIO_P(maska)
  420. GET_GPIO_P(maskb)
  421. #ifdef CONFIG_PM
  422. static unsigned short wakeup_map[GPIO_BANK_NUM];
  423. static unsigned char wakeup_flags_map[MAX_BLACKFIN_GPIOS];
  424. static const unsigned int sic_iwr_irqs[] = {
  425. #if defined(BF533_FAMILY)
  426. IRQ_PROG_INTB
  427. #elif defined(BF537_FAMILY)
  428. IRQ_PROG_INTB, IRQ_PORTG_INTB, IRQ_MAC_TX
  429. #elif defined(BF538_FAMILY)
  430. IRQ_PORTF_INTB
  431. #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
  432. IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB
  433. #elif defined(BF561_FAMILY)
  434. IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB
  435. #else
  436. # error no SIC_IWR defined
  437. #endif
  438. };
  439. /***********************************************************
  440. *
  441. * FUNCTIONS: Blackfin PM Setup API
  442. *
  443. * INPUTS/OUTPUTS:
  444. * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
  445. * type -
  446. * PM_WAKE_RISING
  447. * PM_WAKE_FALLING
  448. * PM_WAKE_HIGH
  449. * PM_WAKE_LOW
  450. * PM_WAKE_BOTH_EDGES
  451. *
  452. * DESCRIPTION: Blackfin PM Driver API
  453. *
  454. * CAUTION:
  455. *************************************************************
  456. * MODIFICATION HISTORY :
  457. **************************************************************/
  458. int gpio_pm_wakeup_request(unsigned gpio, unsigned char type)
  459. {
  460. unsigned long flags;
  461. if ((check_gpio(gpio) < 0) || !type)
  462. return -EINVAL;
  463. local_irq_save_hw(flags);
  464. wakeup_map[gpio_bank(gpio)] |= gpio_bit(gpio);
  465. wakeup_flags_map[gpio] = type;
  466. local_irq_restore_hw(flags);
  467. return 0;
  468. }
  469. EXPORT_SYMBOL(gpio_pm_wakeup_request);
  470. void gpio_pm_wakeup_free(unsigned gpio)
  471. {
  472. unsigned long flags;
  473. if (check_gpio(gpio) < 0)
  474. return;
  475. local_irq_save_hw(flags);
  476. wakeup_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
  477. local_irq_restore_hw(flags);
  478. }
  479. EXPORT_SYMBOL(gpio_pm_wakeup_free);
  480. static int bfin_gpio_wakeup_type(unsigned gpio, unsigned char type)
  481. {
  482. port_setup(gpio, GPIO_USAGE);
  483. set_gpio_dir(gpio, 0);
  484. set_gpio_inen(gpio, 1);
  485. if (type & (PM_WAKE_RISING | PM_WAKE_FALLING))
  486. set_gpio_edge(gpio, 1);
  487. else
  488. set_gpio_edge(gpio, 0);
  489. if ((type & (PM_WAKE_BOTH_EDGES)) == (PM_WAKE_BOTH_EDGES))
  490. set_gpio_both(gpio, 1);
  491. else
  492. set_gpio_both(gpio, 0);
  493. if ((type & (PM_WAKE_FALLING | PM_WAKE_LOW)))
  494. set_gpio_polar(gpio, 1);
  495. else
  496. set_gpio_polar(gpio, 0);
  497. SSYNC();
  498. return 0;
  499. }
  500. u32 bfin_pm_standby_setup(void)
  501. {
  502. u16 bank, mask, i, gpio;
  503. for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
  504. mask = wakeup_map[gpio_bank(i)];
  505. bank = gpio_bank(i);
  506. gpio_bank_saved[bank].maskb = gpio_array[bank]->maskb;
  507. gpio_array[bank]->maskb = 0;
  508. if (mask) {
  509. #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
  510. gpio_bank_saved[bank].fer = *port_fer[bank];
  511. #endif
  512. gpio_bank_saved[bank].inen = gpio_array[bank]->inen;
  513. gpio_bank_saved[bank].polar = gpio_array[bank]->polar;
  514. gpio_bank_saved[bank].dir = gpio_array[bank]->dir;
  515. gpio_bank_saved[bank].edge = gpio_array[bank]->edge;
  516. gpio_bank_saved[bank].both = gpio_array[bank]->both;
  517. gpio_bank_saved[bank].reserved =
  518. reserved_gpio_map[bank];
  519. gpio = i;
  520. while (mask) {
  521. if ((mask & 1) && (wakeup_flags_map[gpio] !=
  522. PM_WAKE_IGNORE)) {
  523. reserved_gpio_map[gpio_bank(gpio)] |=
  524. gpio_bit(gpio);
  525. bfin_gpio_wakeup_type(gpio,
  526. wakeup_flags_map[gpio]);
  527. set_gpio_data(gpio, 0); /*Clear*/
  528. }
  529. gpio++;
  530. mask >>= 1;
  531. }
  532. bfin_internal_set_wake(sic_iwr_irqs[bank], 1);
  533. gpio_array[bank]->maskb_set = wakeup_map[gpio_bank(i)];
  534. }
  535. }
  536. AWA_DUMMY_READ(maskb_set);
  537. return 0;
  538. }
  539. void bfin_pm_standby_restore(void)
  540. {
  541. u16 bank, mask, i;
  542. for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
  543. mask = wakeup_map[gpio_bank(i)];
  544. bank = gpio_bank(i);
  545. if (mask) {
  546. #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
  547. *port_fer[bank] = gpio_bank_saved[bank].fer;
  548. #endif
  549. gpio_array[bank]->inen = gpio_bank_saved[bank].inen;
  550. gpio_array[bank]->dir = gpio_bank_saved[bank].dir;
  551. gpio_array[bank]->polar = gpio_bank_saved[bank].polar;
  552. gpio_array[bank]->edge = gpio_bank_saved[bank].edge;
  553. gpio_array[bank]->both = gpio_bank_saved[bank].both;
  554. reserved_gpio_map[bank] =
  555. gpio_bank_saved[bank].reserved;
  556. bfin_internal_set_wake(sic_iwr_irqs[bank], 0);
  557. }
  558. gpio_array[bank]->maskb = gpio_bank_saved[bank].maskb;
  559. }
  560. AWA_DUMMY_READ(maskb);
  561. }
  562. void bfin_gpio_pm_hibernate_suspend(void)
  563. {
  564. int i, bank;
  565. for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
  566. bank = gpio_bank(i);
  567. #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
  568. gpio_bank_saved[bank].fer = *port_fer[bank];
  569. #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
  570. gpio_bank_saved[bank].mux = *port_mux[bank];
  571. #else
  572. if (bank == 0)
  573. gpio_bank_saved[bank].mux = bfin_read_PORT_MUX();
  574. #endif
  575. #endif
  576. gpio_bank_saved[bank].data = gpio_array[bank]->data;
  577. gpio_bank_saved[bank].inen = gpio_array[bank]->inen;
  578. gpio_bank_saved[bank].polar = gpio_array[bank]->polar;
  579. gpio_bank_saved[bank].dir = gpio_array[bank]->dir;
  580. gpio_bank_saved[bank].edge = gpio_array[bank]->edge;
  581. gpio_bank_saved[bank].both = gpio_array[bank]->both;
  582. gpio_bank_saved[bank].maska = gpio_array[bank]->maska;
  583. }
  584. AWA_DUMMY_READ(maska);
  585. }
  586. void bfin_gpio_pm_hibernate_restore(void)
  587. {
  588. int i, bank;
  589. for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
  590. bank = gpio_bank(i);
  591. #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
  592. #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
  593. *port_mux[bank] = gpio_bank_saved[bank].mux;
  594. #else
  595. if (bank == 0)
  596. bfin_write_PORT_MUX(gpio_bank_saved[bank].mux);
  597. #endif
  598. *port_fer[bank] = gpio_bank_saved[bank].fer;
  599. #endif
  600. gpio_array[bank]->inen = gpio_bank_saved[bank].inen;
  601. gpio_array[bank]->data_set = gpio_bank_saved[bank].data
  602. & gpio_bank_saved[bank].dir;
  603. gpio_array[bank]->dir = gpio_bank_saved[bank].dir;
  604. gpio_array[bank]->polar = gpio_bank_saved[bank].polar;
  605. gpio_array[bank]->edge = gpio_bank_saved[bank].edge;
  606. gpio_array[bank]->both = gpio_bank_saved[bank].both;
  607. gpio_array[bank]->maska = gpio_bank_saved[bank].maska;
  608. }
  609. AWA_DUMMY_READ(maska);
  610. }
  611. #endif
  612. #else /* CONFIG_BF54x */
  613. #ifdef CONFIG_PM
  614. u32 bfin_pm_standby_setup(void)
  615. {
  616. return 0;
  617. }
  618. void bfin_pm_standby_restore(void)
  619. {
  620. }
  621. void bfin_gpio_pm_hibernate_suspend(void)
  622. {
  623. int i, bank;
  624. for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
  625. bank = gpio_bank(i);
  626. gpio_bank_saved[bank].fer = gpio_array[bank]->port_fer;
  627. gpio_bank_saved[bank].mux = gpio_array[bank]->port_mux;
  628. gpio_bank_saved[bank].data = gpio_array[bank]->data;
  629. gpio_bank_saved[bank].inen = gpio_array[bank]->inen;
  630. gpio_bank_saved[bank].dir = gpio_array[bank]->dir_set;
  631. }
  632. }
  633. void bfin_gpio_pm_hibernate_restore(void)
  634. {
  635. int i, bank;
  636. for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
  637. bank = gpio_bank(i);
  638. gpio_array[bank]->port_mux = gpio_bank_saved[bank].mux;
  639. gpio_array[bank]->port_fer = gpio_bank_saved[bank].fer;
  640. gpio_array[bank]->inen = gpio_bank_saved[bank].inen;
  641. gpio_array[bank]->dir_set = gpio_bank_saved[bank].dir;
  642. gpio_array[bank]->data_set = gpio_bank_saved[bank].data
  643. | gpio_bank_saved[bank].dir;
  644. }
  645. }
  646. #endif
  647. unsigned short get_gpio_dir(unsigned gpio)
  648. {
  649. return (0x01 & (gpio_array[gpio_bank(gpio)]->dir_clear >> gpio_sub_n(gpio)));
  650. }
  651. EXPORT_SYMBOL(get_gpio_dir);
  652. #endif /* CONFIG_BF54x */
  653. /***********************************************************
  654. *
  655. * FUNCTIONS: Blackfin Peripheral Resource Allocation
  656. * and PortMux Setup
  657. *
  658. * INPUTS/OUTPUTS:
  659. * per Peripheral Identifier
  660. * label String
  661. *
  662. * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
  663. *
  664. * CAUTION:
  665. *************************************************************
  666. * MODIFICATION HISTORY :
  667. **************************************************************/
  668. int peripheral_request(unsigned short per, const char *label)
  669. {
  670. unsigned long flags;
  671. unsigned short ident = P_IDENT(per);
  672. /*
  673. * Don't cares are pins with only one dedicated function
  674. */
  675. if (per & P_DONTCARE)
  676. return 0;
  677. if (!(per & P_DEFINED))
  678. return -ENODEV;
  679. BUG_ON(ident >= MAX_RESOURCES);
  680. local_irq_save_hw(flags);
  681. /* If a pin can be muxed as either GPIO or peripheral, make
  682. * sure it is not already a GPIO pin when we request it.
  683. */
  684. if (unlikely(!check_gpio(ident) &&
  685. reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
  686. if (system_state == SYSTEM_BOOTING)
  687. dump_stack();
  688. printk(KERN_ERR
  689. "%s: Peripheral %d is already reserved as GPIO by %s !\n",
  690. __func__, ident, get_label(ident));
  691. local_irq_restore_hw(flags);
  692. return -EBUSY;
  693. }
  694. if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
  695. /*
  696. * Pin functions like AMC address strobes my
  697. * be requested and used by several drivers
  698. */
  699. #ifdef CONFIG_BF54x
  700. if (!((per & P_MAYSHARE) && get_portmux(per) == P_FUNCT2MUX(per))) {
  701. #else
  702. if (!(per & P_MAYSHARE)) {
  703. #endif
  704. /*
  705. * Allow that the identical pin function can
  706. * be requested from the same driver twice
  707. */
  708. if (cmp_label(ident, label) == 0)
  709. goto anyway;
  710. if (system_state == SYSTEM_BOOTING)
  711. dump_stack();
  712. printk(KERN_ERR
  713. "%s: Peripheral %d function %d is already reserved by %s !\n",
  714. __func__, ident, P_FUNCT2MUX(per), get_label(ident));
  715. local_irq_restore_hw(flags);
  716. return -EBUSY;
  717. }
  718. }
  719. anyway:
  720. reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
  721. portmux_setup(per);
  722. port_setup(ident, PERIPHERAL_USAGE);
  723. local_irq_restore_hw(flags);
  724. set_label(ident, label);
  725. return 0;
  726. }
  727. EXPORT_SYMBOL(peripheral_request);
  728. int peripheral_request_list(const unsigned short per[], const char *label)
  729. {
  730. u16 cnt;
  731. int ret;
  732. for (cnt = 0; per[cnt] != 0; cnt++) {
  733. ret = peripheral_request(per[cnt], label);
  734. if (ret < 0) {
  735. for ( ; cnt > 0; cnt--)
  736. peripheral_free(per[cnt - 1]);
  737. return ret;
  738. }
  739. }
  740. return 0;
  741. }
  742. EXPORT_SYMBOL(peripheral_request_list);
  743. void peripheral_free(unsigned short per)
  744. {
  745. unsigned long flags;
  746. unsigned short ident = P_IDENT(per);
  747. if (per & P_DONTCARE)
  748. return;
  749. if (!(per & P_DEFINED))
  750. return;
  751. local_irq_save_hw(flags);
  752. if (unlikely(!(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident)))) {
  753. local_irq_restore_hw(flags);
  754. return;
  755. }
  756. if (!(per & P_MAYSHARE))
  757. port_setup(ident, GPIO_USAGE);
  758. reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
  759. set_label(ident, "free");
  760. local_irq_restore_hw(flags);
  761. }
  762. EXPORT_SYMBOL(peripheral_free);
  763. void peripheral_free_list(const unsigned short per[])
  764. {
  765. u16 cnt;
  766. for (cnt = 0; per[cnt] != 0; cnt++)
  767. peripheral_free(per[cnt]);
  768. }
  769. EXPORT_SYMBOL(peripheral_free_list);
  770. /***********************************************************
  771. *
  772. * FUNCTIONS: Blackfin GPIO Driver
  773. *
  774. * INPUTS/OUTPUTS:
  775. * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
  776. * label String
  777. *
  778. * DESCRIPTION: Blackfin GPIO Driver API
  779. *
  780. * CAUTION:
  781. *************************************************************
  782. * MODIFICATION HISTORY :
  783. **************************************************************/
  784. int bfin_gpio_request(unsigned gpio, const char *label)
  785. {
  786. unsigned long flags;
  787. if (check_gpio(gpio) < 0)
  788. return -EINVAL;
  789. local_irq_save_hw(flags);
  790. /*
  791. * Allow that the identical GPIO can
  792. * be requested from the same driver twice
  793. * Do nothing and return -
  794. */
  795. if (cmp_label(gpio, label) == 0) {
  796. local_irq_restore_hw(flags);
  797. return 0;
  798. }
  799. if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
  800. if (system_state == SYSTEM_BOOTING)
  801. dump_stack();
  802. printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
  803. gpio, get_label(gpio));
  804. local_irq_restore_hw(flags);
  805. return -EBUSY;
  806. }
  807. if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
  808. if (system_state == SYSTEM_BOOTING)
  809. dump_stack();
  810. printk(KERN_ERR
  811. "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
  812. gpio, get_label(gpio));
  813. local_irq_restore_hw(flags);
  814. return -EBUSY;
  815. }
  816. if (unlikely(reserved_gpio_irq_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
  817. printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved as gpio-irq!"
  818. " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio);
  819. }
  820. #ifndef CONFIG_BF54x
  821. else { /* Reset POLAR setting when acquiring a gpio for the first time */
  822. set_gpio_polar(gpio, 0);
  823. }
  824. #endif
  825. reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
  826. set_label(gpio, label);
  827. local_irq_restore_hw(flags);
  828. port_setup(gpio, GPIO_USAGE);
  829. return 0;
  830. }
  831. EXPORT_SYMBOL(bfin_gpio_request);
  832. void bfin_gpio_free(unsigned gpio)
  833. {
  834. unsigned long flags;
  835. if (check_gpio(gpio) < 0)
  836. return;
  837. might_sleep();
  838. local_irq_save_hw(flags);
  839. if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
  840. if (system_state == SYSTEM_BOOTING)
  841. dump_stack();
  842. gpio_error(gpio);
  843. local_irq_restore_hw(flags);
  844. return;
  845. }
  846. reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
  847. set_label(gpio, "free");
  848. local_irq_restore_hw(flags);
  849. }
  850. EXPORT_SYMBOL(bfin_gpio_free);
  851. #ifdef BFIN_SPECIAL_GPIO_BANKS
  852. static unsigned short reserved_special_gpio_map[gpio_bank(MAX_RESOURCES)];
  853. int bfin_special_gpio_request(unsigned gpio, const char *label)
  854. {
  855. unsigned long flags;
  856. local_irq_save_hw(flags);
  857. /*
  858. * Allow that the identical GPIO can
  859. * be requested from the same driver twice
  860. * Do nothing and return -
  861. */
  862. if (cmp_label(gpio, label) == 0) {
  863. local_irq_restore_hw(flags);
  864. return 0;
  865. }
  866. if (unlikely(reserved_special_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
  867. local_irq_restore_hw(flags);
  868. printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
  869. gpio, get_label(gpio));
  870. return -EBUSY;
  871. }
  872. if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
  873. local_irq_restore_hw(flags);
  874. printk(KERN_ERR
  875. "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
  876. gpio, get_label(gpio));
  877. return -EBUSY;
  878. }
  879. reserved_special_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
  880. reserved_peri_map[gpio_bank(gpio)] |= gpio_bit(gpio);
  881. set_label(gpio, label);
  882. local_irq_restore_hw(flags);
  883. port_setup(gpio, GPIO_USAGE);
  884. return 0;
  885. }
  886. EXPORT_SYMBOL(bfin_special_gpio_request);
  887. void bfin_special_gpio_free(unsigned gpio)
  888. {
  889. unsigned long flags;
  890. might_sleep();
  891. local_irq_save_hw(flags);
  892. if (unlikely(!(reserved_special_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
  893. gpio_error(gpio);
  894. local_irq_restore_hw(flags);
  895. return;
  896. }
  897. reserved_special_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
  898. reserved_peri_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
  899. set_label(gpio, "free");
  900. local_irq_restore_hw(flags);
  901. }
  902. EXPORT_SYMBOL(bfin_special_gpio_free);
  903. #endif
  904. int bfin_gpio_irq_request(unsigned gpio, const char *label)
  905. {
  906. unsigned long flags;
  907. if (check_gpio(gpio) < 0)
  908. return -EINVAL;
  909. local_irq_save_hw(flags);
  910. if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
  911. if (system_state == SYSTEM_BOOTING)
  912. dump_stack();
  913. printk(KERN_ERR
  914. "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
  915. gpio, get_label(gpio));
  916. local_irq_restore_hw(flags);
  917. return -EBUSY;
  918. }
  919. if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))
  920. printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved by %s! "
  921. "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
  922. gpio, get_label(gpio));
  923. reserved_gpio_irq_map[gpio_bank(gpio)] |= gpio_bit(gpio);
  924. set_label(gpio, label);
  925. local_irq_restore_hw(flags);
  926. port_setup(gpio, GPIO_USAGE);
  927. return 0;
  928. }
  929. void bfin_gpio_irq_free(unsigned gpio)
  930. {
  931. unsigned long flags;
  932. if (check_gpio(gpio) < 0)
  933. return;
  934. local_irq_save_hw(flags);
  935. if (unlikely(!(reserved_gpio_irq_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
  936. if (system_state == SYSTEM_BOOTING)
  937. dump_stack();
  938. gpio_error(gpio);
  939. local_irq_restore_hw(flags);
  940. return;
  941. }
  942. reserved_gpio_irq_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
  943. set_label(gpio, "free");
  944. local_irq_restore_hw(flags);
  945. }
  946. static inline void __bfin_gpio_direction_input(unsigned gpio)
  947. {
  948. #ifdef CONFIG_BF54x
  949. gpio_array[gpio_bank(gpio)]->dir_clear = gpio_bit(gpio);
  950. #else
  951. gpio_array[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
  952. #endif
  953. gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
  954. }
  955. int bfin_gpio_direction_input(unsigned gpio)
  956. {
  957. unsigned long flags;
  958. if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
  959. gpio_error(gpio);
  960. return -EINVAL;
  961. }
  962. local_irq_save_hw(flags);
  963. __bfin_gpio_direction_input(gpio);
  964. AWA_DUMMY_READ(inen);
  965. local_irq_restore_hw(flags);
  966. return 0;
  967. }
  968. EXPORT_SYMBOL(bfin_gpio_direction_input);
  969. void bfin_gpio_irq_prepare(unsigned gpio)
  970. {
  971. #ifdef CONFIG_BF54x
  972. unsigned long flags;
  973. #endif
  974. port_setup(gpio, GPIO_USAGE);
  975. #ifdef CONFIG_BF54x
  976. local_irq_save_hw(flags);
  977. __bfin_gpio_direction_input(gpio);
  978. local_irq_restore_hw(flags);
  979. #endif
  980. }
  981. void bfin_gpio_set_value(unsigned gpio, int arg)
  982. {
  983. if (arg)
  984. gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
  985. else
  986. gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
  987. }
  988. EXPORT_SYMBOL(bfin_gpio_set_value);
  989. int bfin_gpio_direction_output(unsigned gpio, int value)
  990. {
  991. unsigned long flags;
  992. if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
  993. gpio_error(gpio);
  994. return -EINVAL;
  995. }
  996. local_irq_save_hw(flags);
  997. gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
  998. gpio_set_value(gpio, value);
  999. #ifdef CONFIG_BF54x
  1000. gpio_array[gpio_bank(gpio)]->dir_set = gpio_bit(gpio);
  1001. #else
  1002. gpio_array[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
  1003. #endif
  1004. AWA_DUMMY_READ(dir);
  1005. local_irq_restore_hw(flags);
  1006. return 0;
  1007. }
  1008. EXPORT_SYMBOL(bfin_gpio_direction_output);
  1009. int bfin_gpio_get_value(unsigned gpio)
  1010. {
  1011. #ifdef CONFIG_BF54x
  1012. return (1 & (gpio_array[gpio_bank(gpio)]->data >> gpio_sub_n(gpio)));
  1013. #else
  1014. unsigned long flags;
  1015. if (unlikely(get_gpio_edge(gpio))) {
  1016. int ret;
  1017. local_irq_save_hw(flags);
  1018. set_gpio_edge(gpio, 0);
  1019. ret = get_gpio_data(gpio);
  1020. set_gpio_edge(gpio, 1);
  1021. local_irq_restore_hw(flags);
  1022. return ret;
  1023. } else
  1024. return get_gpio_data(gpio);
  1025. #endif
  1026. }
  1027. EXPORT_SYMBOL(bfin_gpio_get_value);
  1028. /* If we are booting from SPI and our board lacks a strong enough pull up,
  1029. * the core can reset and execute the bootrom faster than the resistor can
  1030. * pull the signal logically high. To work around this (common) error in
  1031. * board design, we explicitly set the pin back to GPIO mode, force /CS
  1032. * high, and wait for the electrons to do their thing.
  1033. *
  1034. * This function only makes sense to be called from reset code, but it
  1035. * lives here as we need to force all the GPIO states w/out going through
  1036. * BUG() checks and such.
  1037. */
  1038. void bfin_reset_boot_spi_cs(unsigned short pin)
  1039. {
  1040. unsigned short gpio = P_IDENT(pin);
  1041. port_setup(gpio, GPIO_USAGE);
  1042. gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
  1043. AWA_DUMMY_READ(data_set);
  1044. udelay(1);
  1045. }
  1046. #if defined(CONFIG_PROC_FS)
  1047. static int gpio_proc_read(char *buf, char **start, off_t offset,
  1048. int len, int *unused_i, void *unused_v)
  1049. {
  1050. int c, irq, gpio, outlen = 0;
  1051. for (c = 0; c < MAX_RESOURCES; c++) {
  1052. irq = reserved_gpio_irq_map[gpio_bank(c)] & gpio_bit(c);
  1053. gpio = reserved_gpio_map[gpio_bank(c)] & gpio_bit(c);
  1054. if (!check_gpio(c) && (gpio || irq))
  1055. len = sprintf(buf, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c,
  1056. get_label(c), (gpio && irq) ? " *" : "",
  1057. get_gpio_dir(c) ? "OUTPUT" : "INPUT");
  1058. else if (reserved_peri_map[gpio_bank(c)] & gpio_bit(c))
  1059. len = sprintf(buf, "GPIO_%d: \t%s \t\tPeripheral\n", c, get_label(c));
  1060. else
  1061. continue;
  1062. buf += len;
  1063. outlen += len;
  1064. }
  1065. return outlen;
  1066. }
  1067. static __init int gpio_register_proc(void)
  1068. {
  1069. struct proc_dir_entry *proc_gpio;
  1070. proc_gpio = create_proc_entry("gpio", S_IRUGO, NULL);
  1071. if (proc_gpio)
  1072. proc_gpio->read_proc = gpio_proc_read;
  1073. return proc_gpio != NULL;
  1074. }
  1075. __initcall(gpio_register_proc);
  1076. #endif
  1077. #ifdef CONFIG_GPIOLIB
  1078. int bfin_gpiolib_direction_input(struct gpio_chip *chip, unsigned gpio)
  1079. {
  1080. return bfin_gpio_direction_input(gpio);
  1081. }
  1082. int bfin_gpiolib_direction_output(struct gpio_chip *chip, unsigned gpio, int level)
  1083. {
  1084. return bfin_gpio_direction_output(gpio, level);
  1085. }
  1086. int bfin_gpiolib_get_value(struct gpio_chip *chip, unsigned gpio)
  1087. {
  1088. return bfin_gpio_get_value(gpio);
  1089. }
  1090. void bfin_gpiolib_set_value(struct gpio_chip *chip, unsigned gpio, int value)
  1091. {
  1092. return bfin_gpio_set_value(gpio, value);
  1093. }
  1094. int bfin_gpiolib_gpio_request(struct gpio_chip *chip, unsigned gpio)
  1095. {
  1096. return bfin_gpio_request(gpio, chip->label);
  1097. }
  1098. void bfin_gpiolib_gpio_free(struct gpio_chip *chip, unsigned gpio)
  1099. {
  1100. return bfin_gpio_free(gpio);
  1101. }
  1102. static struct gpio_chip bfin_chip = {
  1103. .label = "Blackfin-GPIOlib",
  1104. .direction_input = bfin_gpiolib_direction_input,
  1105. .get = bfin_gpiolib_get_value,
  1106. .direction_output = bfin_gpiolib_direction_output,
  1107. .set = bfin_gpiolib_set_value,
  1108. .request = bfin_gpiolib_gpio_request,
  1109. .free = bfin_gpiolib_gpio_free,
  1110. .base = 0,
  1111. .ngpio = MAX_BLACKFIN_GPIOS,
  1112. };
  1113. static int __init bfin_gpiolib_setup(void)
  1114. {
  1115. return gpiochip_add(&bfin_chip);
  1116. }
  1117. arch_initcall(bfin_gpiolib_setup);
  1118. #endif