bfin_gpio.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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 <linux/delay.h>
  9. #include <linux/module.h>
  10. #include <linux/err.h>
  11. #include <linux/proc_fs.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/gpio.h>
  14. #include <linux/irq.h>
  15. #if ANOMALY_05000311 || ANOMALY_05000323
  16. enum {
  17. AWA_data = SYSCR,
  18. AWA_data_clear = SYSCR,
  19. AWA_data_set = SYSCR,
  20. AWA_toggle = SYSCR,
  21. AWA_maska = BFIN_UART_SCR,
  22. AWA_maska_clear = BFIN_UART_SCR,
  23. AWA_maska_set = BFIN_UART_SCR,
  24. AWA_maska_toggle = BFIN_UART_SCR,
  25. AWA_maskb = BFIN_UART_GCTL,
  26. AWA_maskb_clear = BFIN_UART_GCTL,
  27. AWA_maskb_set = BFIN_UART_GCTL,
  28. AWA_maskb_toggle = BFIN_UART_GCTL,
  29. AWA_dir = SPORT1_STAT,
  30. AWA_polar = SPORT1_STAT,
  31. AWA_edge = SPORT1_STAT,
  32. AWA_both = SPORT1_STAT,
  33. #if ANOMALY_05000311
  34. AWA_inen = TIMER_ENABLE,
  35. #elif ANOMALY_05000323
  36. AWA_inen = DMA1_1_CONFIG,
  37. #endif
  38. };
  39. /* Anomaly Workaround */
  40. #define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
  41. #else
  42. #define AWA_DUMMY_READ(...) do { } while (0)
  43. #endif
  44. static struct gpio_port_t * const gpio_array[] = {
  45. #if defined(BF533_FAMILY) || defined(BF538_FAMILY)
  46. (struct gpio_port_t *) FIO_FLAG_D,
  47. #elif defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
  48. (struct gpio_port_t *) PORTFIO,
  49. (struct gpio_port_t *) PORTGIO,
  50. (struct gpio_port_t *) PORTHIO,
  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. #else
  56. # error no gpio arrays defined
  57. #endif
  58. };
  59. #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
  60. static unsigned short * const port_fer[] = {
  61. (unsigned short *) PORTF_FER,
  62. (unsigned short *) PORTG_FER,
  63. (unsigned short *) PORTH_FER,
  64. };
  65. # if !defined(BF537_FAMILY)
  66. static unsigned short * const port_mux[] = {
  67. (unsigned short *) PORTF_MUX,
  68. (unsigned short *) PORTG_MUX,
  69. (unsigned short *) PORTH_MUX,
  70. };
  71. static const
  72. u8 pmux_offset[][16] = {
  73. # if defined(CONFIG_BF52x)
  74. { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
  75. { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
  76. { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
  77. # elif defined(CONFIG_BF51x)
  78. { 0, 2, 2, 2, 2, 2, 2, 4, 6, 6, 6, 8, 8, 8, 8, 10 }, /* PORTF */
  79. { 0, 0, 0, 2, 4, 6, 6, 6, 8, 10, 10, 12, 14, 14, 14, 14 }, /* PORTG */
  80. { 0, 0, 0, 0, 2, 2, 4, 6, 10, 10, 10, 10, 10, 10, 10, 10 }, /* PORTH */
  81. # endif
  82. };
  83. # endif
  84. #elif defined(BF538_FAMILY)
  85. static unsigned short * const port_fer[] = {
  86. (unsigned short *) PORTCIO_FER,
  87. (unsigned short *) PORTDIO_FER,
  88. (unsigned short *) PORTEIO_FER,
  89. };
  90. #endif
  91. #define RESOURCE_LABEL_SIZE 16
  92. static struct str_ident {
  93. char name[RESOURCE_LABEL_SIZE];
  94. } str_ident[MAX_RESOURCES];
  95. #if defined(CONFIG_PM)
  96. static struct gpio_port_s gpio_bank_saved[GPIO_BANK_NUM];
  97. # ifdef BF538_FAMILY
  98. static unsigned short port_fer_saved[3];
  99. # endif
  100. #endif
  101. static void gpio_error(unsigned gpio)
  102. {
  103. printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
  104. }
  105. static void set_label(unsigned short ident, const char *label)
  106. {
  107. if (label) {
  108. strncpy(str_ident[ident].name, label,
  109. RESOURCE_LABEL_SIZE);
  110. str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
  111. }
  112. }
  113. static char *get_label(unsigned short ident)
  114. {
  115. return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
  116. }
  117. static int cmp_label(unsigned short ident, const char *label)
  118. {
  119. if (label == NULL) {
  120. dump_stack();
  121. printk(KERN_ERR "Please provide none-null label\n");
  122. }
  123. if (label)
  124. return strcmp(str_ident[ident].name, label);
  125. else
  126. return -EINVAL;
  127. }
  128. #define map_entry(m, i) reserved_##m##_map[gpio_bank(i)]
  129. #define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
  130. #define reserve(m, i) (map_entry(m, i) |= gpio_bit(i))
  131. #define unreserve(m, i) (map_entry(m, i) &= ~gpio_bit(i))
  132. #define DECLARE_RESERVED_MAP(m, c) static unsigned short reserved_##m##_map[c]
  133. DECLARE_RESERVED_MAP(gpio, GPIO_BANK_NUM);
  134. DECLARE_RESERVED_MAP(peri, DIV_ROUND_UP(MAX_RESOURCES, GPIO_BANKSIZE));
  135. DECLARE_RESERVED_MAP(gpio_irq, GPIO_BANK_NUM);
  136. inline int check_gpio(unsigned gpio)
  137. {
  138. if (gpio >= MAX_BLACKFIN_GPIOS)
  139. return -EINVAL;
  140. return 0;
  141. }
  142. static void port_setup(unsigned gpio, unsigned short usage)
  143. {
  144. #if defined(BF538_FAMILY)
  145. /*
  146. * BF538/9 Port C,D and E are special.
  147. * Inverted PORT_FER polarity on CDE and no PORF_FER on F
  148. * Regular PORT F GPIOs are handled here, CDE are exclusively
  149. * managed by GPIOLIB
  150. */
  151. if (gpio < MAX_BLACKFIN_GPIOS || gpio >= MAX_RESOURCES)
  152. return;
  153. gpio -= MAX_BLACKFIN_GPIOS;
  154. if (usage == GPIO_USAGE)
  155. *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
  156. else
  157. *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
  158. SSYNC();
  159. return;
  160. #endif
  161. if (check_gpio(gpio))
  162. return;
  163. #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
  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. #endif
  170. }
  171. #ifdef BF537_FAMILY
  172. static const s8 port_mux[] = {
  173. [GPIO_PF0] = 3,
  174. [GPIO_PF1] = 3,
  175. [GPIO_PF2] = 4,
  176. [GPIO_PF3] = 4,
  177. [GPIO_PF4] = 5,
  178. [GPIO_PF5] = 6,
  179. [GPIO_PF6] = 7,
  180. [GPIO_PF7] = 8,
  181. [GPIO_PF8 ... GPIO_PF15] = -1,
  182. [GPIO_PG0 ... GPIO_PG7] = -1,
  183. [GPIO_PG8] = 9,
  184. [GPIO_PG9] = 9,
  185. [GPIO_PG10] = 10,
  186. [GPIO_PG11] = 10,
  187. [GPIO_PG12] = 10,
  188. [GPIO_PG13] = 11,
  189. [GPIO_PG14] = 11,
  190. [GPIO_PG15] = 11,
  191. [GPIO_PH0 ... GPIO_PH15] = -1,
  192. [PORT_PJ0 ... PORT_PJ3] = -1,
  193. [PORT_PJ4] = 1,
  194. [PORT_PJ5] = 1,
  195. [PORT_PJ6 ... PORT_PJ9] = -1,
  196. [PORT_PJ10] = 0,
  197. [PORT_PJ11] = 0,
  198. };
  199. static int portmux_group_check(unsigned short per)
  200. {
  201. u16 ident = P_IDENT(per);
  202. u16 function = P_FUNCT2MUX(per);
  203. s8 offset = port_mux[ident];
  204. u16 m, pmux, pfunc, mask;
  205. if (offset < 0)
  206. return 0;
  207. pmux = bfin_read_PORT_MUX();
  208. for (m = 0; m < ARRAY_SIZE(port_mux); ++m) {
  209. if (m == ident)
  210. continue;
  211. if (port_mux[m] != offset)
  212. continue;
  213. if (!is_reserved(peri, m, 1))
  214. continue;
  215. if (offset == 1)
  216. mask = 3;
  217. else
  218. mask = 1;
  219. pfunc = (pmux >> offset) & mask;
  220. if (pfunc != (function & mask)) {
  221. pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
  222. ident, function, m, pfunc);
  223. return -EINVAL;
  224. }
  225. }
  226. return 0;
  227. }
  228. static void portmux_setup(unsigned short per)
  229. {
  230. u16 ident = P_IDENT(per);
  231. u16 function = P_FUNCT2MUX(per);
  232. s8 offset = port_mux[ident];
  233. u16 pmux, mask;
  234. if (offset == -1)
  235. return;
  236. pmux = bfin_read_PORT_MUX();
  237. if (offset == 1)
  238. mask = 3;
  239. else
  240. mask = 1;
  241. pmux &= ~(mask << offset);
  242. pmux |= ((function & mask) << offset);
  243. bfin_write_PORT_MUX(pmux);
  244. }
  245. #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
  246. static int portmux_group_check(unsigned short per)
  247. {
  248. u16 ident = P_IDENT(per);
  249. u16 function = P_FUNCT2MUX(per);
  250. u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
  251. u16 pin, gpiopin, pfunc;
  252. for (pin = 0; pin < GPIO_BANKSIZE; ++pin) {
  253. if (offset != pmux_offset[gpio_bank(ident)][pin])
  254. continue;
  255. gpiopin = gpio_bank(ident) * GPIO_BANKSIZE + pin;
  256. if (gpiopin == ident)
  257. continue;
  258. if (!is_reserved(peri, gpiopin, 1))
  259. continue;
  260. pfunc = *port_mux[gpio_bank(ident)];
  261. pfunc = (pfunc >> offset) & 3;
  262. if (pfunc != function) {
  263. pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
  264. ident, function, gpiopin, pfunc);
  265. return -EINVAL;
  266. }
  267. }
  268. return 0;
  269. }
  270. inline void portmux_setup(unsigned short per)
  271. {
  272. u16 ident = P_IDENT(per);
  273. u16 function = P_FUNCT2MUX(per);
  274. u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
  275. u16 pmux;
  276. pmux = *port_mux[gpio_bank(ident)];
  277. if (((pmux >> offset) & 3) == function)
  278. return;
  279. pmux &= ~(3 << offset);
  280. pmux |= (function & 3) << offset;
  281. *port_mux[gpio_bank(ident)] = pmux;
  282. SSYNC();
  283. }
  284. #else
  285. # define portmux_setup(...) do { } while (0)
  286. static int portmux_group_check(unsigned short per)
  287. {
  288. return 0;
  289. }
  290. #endif
  291. /***********************************************************
  292. *
  293. * FUNCTIONS: Blackfin General Purpose Ports Access Functions
  294. *
  295. * INPUTS/OUTPUTS:
  296. * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
  297. *
  298. *
  299. * DESCRIPTION: These functions abstract direct register access
  300. * to Blackfin processor General Purpose
  301. * Ports Regsiters
  302. *
  303. * CAUTION: These functions do not belong to the GPIO Driver API
  304. *************************************************************
  305. * MODIFICATION HISTORY :
  306. **************************************************************/
  307. /* Set a specific bit */
  308. #define SET_GPIO(name) \
  309. void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
  310. { \
  311. unsigned long flags; \
  312. flags = hard_local_irq_save(); \
  313. if (arg) \
  314. gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
  315. else \
  316. gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
  317. AWA_DUMMY_READ(name); \
  318. hard_local_irq_restore(flags); \
  319. } \
  320. EXPORT_SYMBOL(set_gpio_ ## name);
  321. SET_GPIO(dir) /* set_gpio_dir() */
  322. SET_GPIO(inen) /* set_gpio_inen() */
  323. SET_GPIO(polar) /* set_gpio_polar() */
  324. SET_GPIO(edge) /* set_gpio_edge() */
  325. SET_GPIO(both) /* set_gpio_both() */
  326. #define SET_GPIO_SC(name) \
  327. void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
  328. { \
  329. unsigned long flags; \
  330. if (ANOMALY_05000311 || ANOMALY_05000323) \
  331. flags = hard_local_irq_save(); \
  332. if (arg) \
  333. gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
  334. else \
  335. gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
  336. if (ANOMALY_05000311 || ANOMALY_05000323) { \
  337. AWA_DUMMY_READ(name); \
  338. hard_local_irq_restore(flags); \
  339. } \
  340. } \
  341. EXPORT_SYMBOL(set_gpio_ ## name);
  342. SET_GPIO_SC(maska)
  343. SET_GPIO_SC(maskb)
  344. SET_GPIO_SC(data)
  345. void set_gpio_toggle(unsigned gpio)
  346. {
  347. unsigned long flags;
  348. if (ANOMALY_05000311 || ANOMALY_05000323)
  349. flags = hard_local_irq_save();
  350. gpio_array[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
  351. if (ANOMALY_05000311 || ANOMALY_05000323) {
  352. AWA_DUMMY_READ(toggle);
  353. hard_local_irq_restore(flags);
  354. }
  355. }
  356. EXPORT_SYMBOL(set_gpio_toggle);
  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. flags = hard_local_irq_save(); \
  364. gpio_array[gpio_bank(gpio)]->name = arg; \
  365. if (ANOMALY_05000311 || ANOMALY_05000323) { \
  366. AWA_DUMMY_READ(name); \
  367. hard_local_irq_restore(flags); \
  368. } \
  369. } \
  370. EXPORT_SYMBOL(set_gpiop_ ## name);
  371. SET_GPIO_P(data)
  372. SET_GPIO_P(dir)
  373. SET_GPIO_P(inen)
  374. SET_GPIO_P(polar)
  375. SET_GPIO_P(edge)
  376. SET_GPIO_P(both)
  377. SET_GPIO_P(maska)
  378. SET_GPIO_P(maskb)
  379. /* Get a specific bit */
  380. #define GET_GPIO(name) \
  381. unsigned short get_gpio_ ## name(unsigned gpio) \
  382. { \
  383. unsigned long flags; \
  384. unsigned short ret; \
  385. if (ANOMALY_05000311 || ANOMALY_05000323) \
  386. flags = hard_local_irq_save(); \
  387. ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
  388. if (ANOMALY_05000311 || ANOMALY_05000323) { \
  389. AWA_DUMMY_READ(name); \
  390. hard_local_irq_restore(flags); \
  391. } \
  392. return ret; \
  393. } \
  394. EXPORT_SYMBOL(get_gpio_ ## name);
  395. GET_GPIO(data)
  396. GET_GPIO(dir)
  397. GET_GPIO(inen)
  398. GET_GPIO(polar)
  399. GET_GPIO(edge)
  400. GET_GPIO(both)
  401. GET_GPIO(maska)
  402. GET_GPIO(maskb)
  403. /*Get current PORT date (16-bit word)*/
  404. #define GET_GPIO_P(name) \
  405. unsigned short get_gpiop_ ## name(unsigned gpio) \
  406. { \
  407. unsigned long flags; \
  408. unsigned short ret; \
  409. if (ANOMALY_05000311 || ANOMALY_05000323) \
  410. flags = hard_local_irq_save(); \
  411. ret = (gpio_array[gpio_bank(gpio)]->name); \
  412. if (ANOMALY_05000311 || ANOMALY_05000323) { \
  413. AWA_DUMMY_READ(name); \
  414. hard_local_irq_restore(flags); \
  415. } \
  416. return ret; \
  417. } \
  418. EXPORT_SYMBOL(get_gpiop_ ## name);
  419. GET_GPIO_P(data)
  420. GET_GPIO_P(dir)
  421. GET_GPIO_P(inen)
  422. GET_GPIO_P(polar)
  423. GET_GPIO_P(edge)
  424. GET_GPIO_P(both)
  425. GET_GPIO_P(maska)
  426. GET_GPIO_P(maskb)
  427. #ifdef CONFIG_PM
  428. DECLARE_RESERVED_MAP(wakeup, GPIO_BANK_NUM);
  429. static const unsigned int sic_iwr_irqs[] = {
  430. #if defined(BF533_FAMILY)
  431. IRQ_PROG_INTB
  432. #elif defined(BF537_FAMILY)
  433. IRQ_PF_INTB_WATCH, IRQ_PORTG_INTB, IRQ_PH_INTB_MAC_TX
  434. #elif defined(BF538_FAMILY)
  435. IRQ_PORTF_INTB
  436. #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
  437. IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB
  438. #elif defined(BF561_FAMILY)
  439. IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB
  440. #else
  441. # error no SIC_IWR defined
  442. #endif
  443. };
  444. /***********************************************************
  445. *
  446. * FUNCTIONS: Blackfin PM Setup API
  447. *
  448. * INPUTS/OUTPUTS:
  449. * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
  450. * type -
  451. * PM_WAKE_RISING
  452. * PM_WAKE_FALLING
  453. * PM_WAKE_HIGH
  454. * PM_WAKE_LOW
  455. * PM_WAKE_BOTH_EDGES
  456. *
  457. * DESCRIPTION: Blackfin PM Driver API
  458. *
  459. * CAUTION:
  460. *************************************************************
  461. * MODIFICATION HISTORY :
  462. **************************************************************/
  463. int bfin_gpio_pm_wakeup_ctrl(unsigned gpio, unsigned ctrl)
  464. {
  465. unsigned long flags;
  466. if (check_gpio(gpio) < 0)
  467. return -EINVAL;
  468. flags = hard_local_irq_save();
  469. if (ctrl)
  470. reserve(wakeup, gpio);
  471. else
  472. unreserve(wakeup, gpio);
  473. set_gpio_maskb(gpio, ctrl);
  474. hard_local_irq_restore(flags);
  475. return 0;
  476. }
  477. int bfin_gpio_pm_standby_ctrl(unsigned ctrl)
  478. {
  479. u16 bank, mask, i;
  480. for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
  481. mask = map_entry(wakeup, i);
  482. bank = gpio_bank(i);
  483. if (mask)
  484. bfin_internal_set_wake(sic_iwr_irqs[bank], ctrl);
  485. }
  486. return 0;
  487. }
  488. void bfin_gpio_pm_hibernate_suspend(void)
  489. {
  490. int i, bank;
  491. #ifdef BF538_FAMILY
  492. for (i = 0; i < ARRAY_SIZE(port_fer_saved); ++i)
  493. port_fer_saved[i] = *port_fer[i];
  494. #endif
  495. for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
  496. bank = gpio_bank(i);
  497. #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
  498. gpio_bank_saved[bank].fer = *port_fer[bank];
  499. #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
  500. gpio_bank_saved[bank].mux = *port_mux[bank];
  501. #else
  502. if (bank == 0)
  503. gpio_bank_saved[bank].mux = bfin_read_PORT_MUX();
  504. #endif
  505. #endif
  506. gpio_bank_saved[bank].data = gpio_array[bank]->data;
  507. gpio_bank_saved[bank].inen = gpio_array[bank]->inen;
  508. gpio_bank_saved[bank].polar = gpio_array[bank]->polar;
  509. gpio_bank_saved[bank].dir = gpio_array[bank]->dir;
  510. gpio_bank_saved[bank].edge = gpio_array[bank]->edge;
  511. gpio_bank_saved[bank].both = gpio_array[bank]->both;
  512. gpio_bank_saved[bank].maska = gpio_array[bank]->maska;
  513. }
  514. #ifdef BFIN_SPECIAL_GPIO_BANKS
  515. bfin_special_gpio_pm_hibernate_suspend();
  516. #endif
  517. AWA_DUMMY_READ(maska);
  518. }
  519. void bfin_gpio_pm_hibernate_restore(void)
  520. {
  521. int i, bank;
  522. #ifdef BF538_FAMILY
  523. for (i = 0; i < ARRAY_SIZE(port_fer_saved); ++i)
  524. *port_fer[i] = port_fer_saved[i];
  525. #endif
  526. for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
  527. bank = gpio_bank(i);
  528. #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
  529. #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
  530. *port_mux[bank] = gpio_bank_saved[bank].mux;
  531. #else
  532. if (bank == 0)
  533. bfin_write_PORT_MUX(gpio_bank_saved[bank].mux);
  534. #endif
  535. *port_fer[bank] = gpio_bank_saved[bank].fer;
  536. #endif
  537. gpio_array[bank]->inen = gpio_bank_saved[bank].inen;
  538. gpio_array[bank]->data_set = gpio_bank_saved[bank].data
  539. & gpio_bank_saved[bank].dir;
  540. gpio_array[bank]->dir = gpio_bank_saved[bank].dir;
  541. gpio_array[bank]->polar = gpio_bank_saved[bank].polar;
  542. gpio_array[bank]->edge = gpio_bank_saved[bank].edge;
  543. gpio_array[bank]->both = gpio_bank_saved[bank].both;
  544. gpio_array[bank]->maska = gpio_bank_saved[bank].maska;
  545. }
  546. #ifdef BFIN_SPECIAL_GPIO_BANKS
  547. bfin_special_gpio_pm_hibernate_restore();
  548. #endif
  549. AWA_DUMMY_READ(maska);
  550. }
  551. #endif
  552. /***********************************************************
  553. *
  554. * FUNCTIONS: Blackfin Peripheral Resource Allocation
  555. * and PortMux Setup
  556. *
  557. * INPUTS/OUTPUTS:
  558. * per Peripheral Identifier
  559. * label String
  560. *
  561. * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
  562. *
  563. * CAUTION:
  564. *************************************************************
  565. * MODIFICATION HISTORY :
  566. **************************************************************/
  567. int peripheral_request(unsigned short per, const char *label)
  568. {
  569. unsigned long flags;
  570. unsigned short ident = P_IDENT(per);
  571. /*
  572. * Don't cares are pins with only one dedicated function
  573. */
  574. if (per & P_DONTCARE)
  575. return 0;
  576. if (!(per & P_DEFINED))
  577. return -ENODEV;
  578. BUG_ON(ident >= MAX_RESOURCES);
  579. flags = hard_local_irq_save();
  580. /* If a pin can be muxed as either GPIO or peripheral, make
  581. * sure it is not already a GPIO pin when we request it.
  582. */
  583. if (unlikely(!check_gpio(ident) && is_reserved(gpio, ident, 1))) {
  584. if (system_state == SYSTEM_BOOTING)
  585. dump_stack();
  586. printk(KERN_ERR
  587. "%s: Peripheral %d is already reserved as GPIO by %s !\n",
  588. __func__, ident, get_label(ident));
  589. hard_local_irq_restore(flags);
  590. return -EBUSY;
  591. }
  592. if (unlikely(is_reserved(peri, ident, 1))) {
  593. /*
  594. * Pin functions like AMC address strobes my
  595. * be requested and used by several drivers
  596. */
  597. if (!(per & P_MAYSHARE)) {
  598. /*
  599. * Allow that the identical pin function can
  600. * be requested from the same driver twice
  601. */
  602. if (cmp_label(ident, label) == 0)
  603. goto anyway;
  604. if (system_state == SYSTEM_BOOTING)
  605. dump_stack();
  606. printk(KERN_ERR
  607. "%s: Peripheral %d function %d is already reserved by %s !\n",
  608. __func__, ident, P_FUNCT2MUX(per), get_label(ident));
  609. hard_local_irq_restore(flags);
  610. return -EBUSY;
  611. }
  612. }
  613. if (unlikely(portmux_group_check(per))) {
  614. hard_local_irq_restore(flags);
  615. return -EBUSY;
  616. }
  617. anyway:
  618. reserve(peri, ident);
  619. portmux_setup(per);
  620. port_setup(ident, PERIPHERAL_USAGE);
  621. hard_local_irq_restore(flags);
  622. set_label(ident, label);
  623. return 0;
  624. }
  625. EXPORT_SYMBOL(peripheral_request);
  626. int peripheral_request_list(const unsigned short per[], const char *label)
  627. {
  628. u16 cnt;
  629. int ret;
  630. for (cnt = 0; per[cnt] != 0; cnt++) {
  631. ret = peripheral_request(per[cnt], label);
  632. if (ret < 0) {
  633. for ( ; cnt > 0; cnt--)
  634. peripheral_free(per[cnt - 1]);
  635. return ret;
  636. }
  637. }
  638. return 0;
  639. }
  640. EXPORT_SYMBOL(peripheral_request_list);
  641. void peripheral_free(unsigned short per)
  642. {
  643. unsigned long flags;
  644. unsigned short ident = P_IDENT(per);
  645. if (per & P_DONTCARE)
  646. return;
  647. if (!(per & P_DEFINED))
  648. return;
  649. flags = hard_local_irq_save();
  650. if (unlikely(!is_reserved(peri, ident, 0))) {
  651. hard_local_irq_restore(flags);
  652. return;
  653. }
  654. if (!(per & P_MAYSHARE))
  655. port_setup(ident, GPIO_USAGE);
  656. unreserve(peri, ident);
  657. set_label(ident, "free");
  658. hard_local_irq_restore(flags);
  659. }
  660. EXPORT_SYMBOL(peripheral_free);
  661. void peripheral_free_list(const unsigned short per[])
  662. {
  663. u16 cnt;
  664. for (cnt = 0; per[cnt] != 0; cnt++)
  665. peripheral_free(per[cnt]);
  666. }
  667. EXPORT_SYMBOL(peripheral_free_list);
  668. /***********************************************************
  669. *
  670. * FUNCTIONS: Blackfin GPIO Driver
  671. *
  672. * INPUTS/OUTPUTS:
  673. * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
  674. * label String
  675. *
  676. * DESCRIPTION: Blackfin GPIO Driver API
  677. *
  678. * CAUTION:
  679. *************************************************************
  680. * MODIFICATION HISTORY :
  681. **************************************************************/
  682. int bfin_gpio_request(unsigned gpio, const char *label)
  683. {
  684. unsigned long flags;
  685. if (check_gpio(gpio) < 0)
  686. return -EINVAL;
  687. flags = hard_local_irq_save();
  688. /*
  689. * Allow that the identical GPIO can
  690. * be requested from the same driver twice
  691. * Do nothing and return -
  692. */
  693. if (cmp_label(gpio, label) == 0) {
  694. hard_local_irq_restore(flags);
  695. return 0;
  696. }
  697. if (unlikely(is_reserved(gpio, gpio, 1))) {
  698. if (system_state == SYSTEM_BOOTING)
  699. dump_stack();
  700. printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
  701. gpio, get_label(gpio));
  702. hard_local_irq_restore(flags);
  703. return -EBUSY;
  704. }
  705. if (unlikely(is_reserved(peri, gpio, 1))) {
  706. if (system_state == SYSTEM_BOOTING)
  707. dump_stack();
  708. printk(KERN_ERR
  709. "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
  710. gpio, get_label(gpio));
  711. hard_local_irq_restore(flags);
  712. return -EBUSY;
  713. }
  714. if (unlikely(is_reserved(gpio_irq, gpio, 1))) {
  715. printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved as gpio-irq!"
  716. " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio);
  717. } else { /* Reset POLAR setting when acquiring a gpio for the first time */
  718. set_gpio_polar(gpio, 0);
  719. }
  720. reserve(gpio, gpio);
  721. set_label(gpio, label);
  722. hard_local_irq_restore(flags);
  723. port_setup(gpio, GPIO_USAGE);
  724. return 0;
  725. }
  726. EXPORT_SYMBOL(bfin_gpio_request);
  727. void bfin_gpio_free(unsigned gpio)
  728. {
  729. unsigned long flags;
  730. if (check_gpio(gpio) < 0)
  731. return;
  732. might_sleep();
  733. flags = hard_local_irq_save();
  734. if (unlikely(!is_reserved(gpio, gpio, 0))) {
  735. if (system_state == SYSTEM_BOOTING)
  736. dump_stack();
  737. gpio_error(gpio);
  738. hard_local_irq_restore(flags);
  739. return;
  740. }
  741. unreserve(gpio, gpio);
  742. set_label(gpio, "free");
  743. hard_local_irq_restore(flags);
  744. }
  745. EXPORT_SYMBOL(bfin_gpio_free);
  746. #ifdef BFIN_SPECIAL_GPIO_BANKS
  747. DECLARE_RESERVED_MAP(special_gpio, gpio_bank(MAX_RESOURCES));
  748. int bfin_special_gpio_request(unsigned gpio, const char *label)
  749. {
  750. unsigned long flags;
  751. flags = hard_local_irq_save();
  752. /*
  753. * Allow that the identical GPIO can
  754. * be requested from the same driver twice
  755. * Do nothing and return -
  756. */
  757. if (cmp_label(gpio, label) == 0) {
  758. hard_local_irq_restore(flags);
  759. return 0;
  760. }
  761. if (unlikely(is_reserved(special_gpio, gpio, 1))) {
  762. hard_local_irq_restore(flags);
  763. printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
  764. gpio, get_label(gpio));
  765. return -EBUSY;
  766. }
  767. if (unlikely(is_reserved(peri, gpio, 1))) {
  768. hard_local_irq_restore(flags);
  769. printk(KERN_ERR
  770. "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
  771. gpio, get_label(gpio));
  772. return -EBUSY;
  773. }
  774. reserve(special_gpio, gpio);
  775. reserve(peri, gpio);
  776. set_label(gpio, label);
  777. hard_local_irq_restore(flags);
  778. port_setup(gpio, GPIO_USAGE);
  779. return 0;
  780. }
  781. EXPORT_SYMBOL(bfin_special_gpio_request);
  782. void bfin_special_gpio_free(unsigned gpio)
  783. {
  784. unsigned long flags;
  785. might_sleep();
  786. flags = hard_local_irq_save();
  787. if (unlikely(!is_reserved(special_gpio, gpio, 0))) {
  788. gpio_error(gpio);
  789. hard_local_irq_restore(flags);
  790. return;
  791. }
  792. unreserve(special_gpio, gpio);
  793. unreserve(peri, gpio);
  794. set_label(gpio, "free");
  795. hard_local_irq_restore(flags);
  796. }
  797. EXPORT_SYMBOL(bfin_special_gpio_free);
  798. #endif
  799. int bfin_gpio_irq_request(unsigned gpio, const char *label)
  800. {
  801. unsigned long flags;
  802. if (check_gpio(gpio) < 0)
  803. return -EINVAL;
  804. flags = hard_local_irq_save();
  805. if (unlikely(is_reserved(peri, gpio, 1))) {
  806. if (system_state == SYSTEM_BOOTING)
  807. dump_stack();
  808. printk(KERN_ERR
  809. "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
  810. gpio, get_label(gpio));
  811. hard_local_irq_restore(flags);
  812. return -EBUSY;
  813. }
  814. if (unlikely(is_reserved(gpio, gpio, 1)))
  815. printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved by %s! "
  816. "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
  817. gpio, get_label(gpio));
  818. reserve(gpio_irq, gpio);
  819. set_label(gpio, label);
  820. hard_local_irq_restore(flags);
  821. port_setup(gpio, GPIO_USAGE);
  822. return 0;
  823. }
  824. void bfin_gpio_irq_free(unsigned gpio)
  825. {
  826. unsigned long flags;
  827. if (check_gpio(gpio) < 0)
  828. return;
  829. flags = hard_local_irq_save();
  830. if (unlikely(!is_reserved(gpio_irq, gpio, 0))) {
  831. if (system_state == SYSTEM_BOOTING)
  832. dump_stack();
  833. gpio_error(gpio);
  834. hard_local_irq_restore(flags);
  835. return;
  836. }
  837. unreserve(gpio_irq, gpio);
  838. set_label(gpio, "free");
  839. hard_local_irq_restore(flags);
  840. }
  841. static inline void __bfin_gpio_direction_input(unsigned gpio)
  842. {
  843. gpio_array[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
  844. gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
  845. }
  846. int bfin_gpio_direction_input(unsigned gpio)
  847. {
  848. unsigned long flags;
  849. if (unlikely(!is_reserved(gpio, gpio, 0))) {
  850. gpio_error(gpio);
  851. return -EINVAL;
  852. }
  853. flags = hard_local_irq_save();
  854. __bfin_gpio_direction_input(gpio);
  855. AWA_DUMMY_READ(inen);
  856. hard_local_irq_restore(flags);
  857. return 0;
  858. }
  859. EXPORT_SYMBOL(bfin_gpio_direction_input);
  860. void bfin_gpio_irq_prepare(unsigned gpio)
  861. {
  862. port_setup(gpio, GPIO_USAGE);
  863. }
  864. void bfin_gpio_set_value(unsigned gpio, int arg)
  865. {
  866. if (arg)
  867. gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
  868. else
  869. gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
  870. }
  871. EXPORT_SYMBOL(bfin_gpio_set_value);
  872. int bfin_gpio_direction_output(unsigned gpio, int value)
  873. {
  874. unsigned long flags;
  875. if (unlikely(!is_reserved(gpio, gpio, 0))) {
  876. gpio_error(gpio);
  877. return -EINVAL;
  878. }
  879. flags = hard_local_irq_save();
  880. gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
  881. gpio_set_value(gpio, value);
  882. gpio_array[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
  883. AWA_DUMMY_READ(dir);
  884. hard_local_irq_restore(flags);
  885. return 0;
  886. }
  887. EXPORT_SYMBOL(bfin_gpio_direction_output);
  888. int bfin_gpio_get_value(unsigned gpio)
  889. {
  890. unsigned long flags;
  891. if (unlikely(get_gpio_edge(gpio))) {
  892. int ret;
  893. flags = hard_local_irq_save();
  894. set_gpio_edge(gpio, 0);
  895. ret = get_gpio_data(gpio);
  896. set_gpio_edge(gpio, 1);
  897. hard_local_irq_restore(flags);
  898. return ret;
  899. } else
  900. return get_gpio_data(gpio);
  901. }
  902. EXPORT_SYMBOL(bfin_gpio_get_value);
  903. /* If we are booting from SPI and our board lacks a strong enough pull up,
  904. * the core can reset and execute the bootrom faster than the resistor can
  905. * pull the signal logically high. To work around this (common) error in
  906. * board design, we explicitly set the pin back to GPIO mode, force /CS
  907. * high, and wait for the electrons to do their thing.
  908. *
  909. * This function only makes sense to be called from reset code, but it
  910. * lives here as we need to force all the GPIO states w/out going through
  911. * BUG() checks and such.
  912. */
  913. void bfin_reset_boot_spi_cs(unsigned short pin)
  914. {
  915. unsigned short gpio = P_IDENT(pin);
  916. port_setup(gpio, GPIO_USAGE);
  917. gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
  918. AWA_DUMMY_READ(data_set);
  919. udelay(1);
  920. }
  921. #if defined(CONFIG_PROC_FS)
  922. static int gpio_proc_show(struct seq_file *m, void *v)
  923. {
  924. int c, irq, gpio;
  925. for (c = 0; c < MAX_RESOURCES; c++) {
  926. irq = is_reserved(gpio_irq, c, 1);
  927. gpio = is_reserved(gpio, c, 1);
  928. if (!check_gpio(c) && (gpio || irq))
  929. seq_printf(m, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c,
  930. get_label(c), (gpio && irq) ? " *" : "",
  931. get_gpio_dir(c) ? "OUTPUT" : "INPUT");
  932. else if (is_reserved(peri, c, 1))
  933. seq_printf(m, "GPIO_%d: \t%s \t\tPeripheral\n", c, get_label(c));
  934. else
  935. continue;
  936. }
  937. return 0;
  938. }
  939. static int gpio_proc_open(struct inode *inode, struct file *file)
  940. {
  941. return single_open(file, gpio_proc_show, NULL);
  942. }
  943. static const struct file_operations gpio_proc_ops = {
  944. .open = gpio_proc_open,
  945. .read = seq_read,
  946. .llseek = seq_lseek,
  947. .release = single_release,
  948. };
  949. static __init int gpio_register_proc(void)
  950. {
  951. struct proc_dir_entry *proc_gpio;
  952. proc_gpio = proc_create("gpio", 0, NULL, &gpio_proc_ops);
  953. return proc_gpio == NULL;
  954. }
  955. __initcall(gpio_register_proc);
  956. #endif
  957. #ifdef CONFIG_GPIOLIB
  958. static int bfin_gpiolib_direction_input(struct gpio_chip *chip, unsigned gpio)
  959. {
  960. return bfin_gpio_direction_input(gpio);
  961. }
  962. static int bfin_gpiolib_direction_output(struct gpio_chip *chip, unsigned gpio, int level)
  963. {
  964. return bfin_gpio_direction_output(gpio, level);
  965. }
  966. static int bfin_gpiolib_get_value(struct gpio_chip *chip, unsigned gpio)
  967. {
  968. return bfin_gpio_get_value(gpio);
  969. }
  970. static void bfin_gpiolib_set_value(struct gpio_chip *chip, unsigned gpio, int value)
  971. {
  972. return bfin_gpio_set_value(gpio, value);
  973. }
  974. static int bfin_gpiolib_gpio_request(struct gpio_chip *chip, unsigned gpio)
  975. {
  976. return bfin_gpio_request(gpio, chip->label);
  977. }
  978. static void bfin_gpiolib_gpio_free(struct gpio_chip *chip, unsigned gpio)
  979. {
  980. return bfin_gpio_free(gpio);
  981. }
  982. static int bfin_gpiolib_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
  983. {
  984. return gpio + GPIO_IRQ_BASE;
  985. }
  986. static struct gpio_chip bfin_chip = {
  987. .label = "BFIN-GPIO",
  988. .direction_input = bfin_gpiolib_direction_input,
  989. .get = bfin_gpiolib_get_value,
  990. .direction_output = bfin_gpiolib_direction_output,
  991. .set = bfin_gpiolib_set_value,
  992. .request = bfin_gpiolib_gpio_request,
  993. .free = bfin_gpiolib_gpio_free,
  994. .to_irq = bfin_gpiolib_gpio_to_irq,
  995. .base = 0,
  996. .ngpio = MAX_BLACKFIN_GPIOS,
  997. };
  998. static int __init bfin_gpiolib_setup(void)
  999. {
  1000. return gpiochip_add(&bfin_chip);
  1001. }
  1002. arch_initcall(bfin_gpiolib_setup);
  1003. #endif