bfin_gpio.c 28 KB

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