bfin_gpio.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. /*
  2. * File: arch/blackfin/kernel/bfin_gpio.c
  3. * Based on:
  4. * Author: Michael Hennerich (hennerich@blackfin.uclinux.org)
  5. *
  6. * Created:
  7. * Description: GPIO Abstraction Layer
  8. *
  9. * Modified:
  10. * Copyright 2006 Analog Devices Inc.
  11. *
  12. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, see the file COPYING, or write
  26. * to the Free Software Foundation, Inc.,
  27. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. /*
  30. * Number BF537/6/4 BF561 BF533/2/1
  31. *
  32. * GPIO_0 PF0 PF0 PF0
  33. * GPIO_1 PF1 PF1 PF1
  34. * GPIO_2 PF2 PF2 PF2
  35. * GPIO_3 PF3 PF3 PF3
  36. * GPIO_4 PF4 PF4 PF4
  37. * GPIO_5 PF5 PF5 PF5
  38. * GPIO_6 PF6 PF6 PF6
  39. * GPIO_7 PF7 PF7 PF7
  40. * GPIO_8 PF8 PF8 PF8
  41. * GPIO_9 PF9 PF9 PF9
  42. * GPIO_10 PF10 PF10 PF10
  43. * GPIO_11 PF11 PF11 PF11
  44. * GPIO_12 PF12 PF12 PF12
  45. * GPIO_13 PF13 PF13 PF13
  46. * GPIO_14 PF14 PF14 PF14
  47. * GPIO_15 PF15 PF15 PF15
  48. * GPIO_16 PG0 PF16
  49. * GPIO_17 PG1 PF17
  50. * GPIO_18 PG2 PF18
  51. * GPIO_19 PG3 PF19
  52. * GPIO_20 PG4 PF20
  53. * GPIO_21 PG5 PF21
  54. * GPIO_22 PG6 PF22
  55. * GPIO_23 PG7 PF23
  56. * GPIO_24 PG8 PF24
  57. * GPIO_25 PG9 PF25
  58. * GPIO_26 PG10 PF26
  59. * GPIO_27 PG11 PF27
  60. * GPIO_28 PG12 PF28
  61. * GPIO_29 PG13 PF29
  62. * GPIO_30 PG14 PF30
  63. * GPIO_31 PG15 PF31
  64. * GPIO_32 PH0 PF32
  65. * GPIO_33 PH1 PF33
  66. * GPIO_34 PH2 PF34
  67. * GPIO_35 PH3 PF35
  68. * GPIO_36 PH4 PF36
  69. * GPIO_37 PH5 PF37
  70. * GPIO_38 PH6 PF38
  71. * GPIO_39 PH7 PF39
  72. * GPIO_40 PH8 PF40
  73. * GPIO_41 PH9 PF41
  74. * GPIO_42 PH10 PF42
  75. * GPIO_43 PH11 PF43
  76. * GPIO_44 PH12 PF44
  77. * GPIO_45 PH13 PF45
  78. * GPIO_46 PH14 PF46
  79. * GPIO_47 PH15 PF47
  80. */
  81. #include <linux/delay.h>
  82. #include <linux/module.h>
  83. #include <linux/err.h>
  84. #include <asm/blackfin.h>
  85. #include <asm/gpio.h>
  86. #include <asm/portmux.h>
  87. #include <linux/irq.h>
  88. #ifdef BF533_FAMILY
  89. static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
  90. (struct gpio_port_t *) FIO_FLAG_D,
  91. };
  92. #endif
  93. #ifdef BF537_FAMILY
  94. static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
  95. (struct gpio_port_t *) PORTFIO,
  96. (struct gpio_port_t *) PORTGIO,
  97. (struct gpio_port_t *) PORTHIO,
  98. };
  99. static unsigned short *port_fer[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
  100. (unsigned short *) PORTF_FER,
  101. (unsigned short *) PORTG_FER,
  102. (unsigned short *) PORTH_FER,
  103. };
  104. #endif
  105. #ifdef BF561_FAMILY
  106. static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
  107. (struct gpio_port_t *) FIO0_FLAG_D,
  108. (struct gpio_port_t *) FIO1_FLAG_D,
  109. (struct gpio_port_t *) FIO2_FLAG_D,
  110. };
  111. #endif
  112. static unsigned short reserved_gpio_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
  113. static unsigned short reserved_peri_map[gpio_bank(MAX_BLACKFIN_GPIOS + 16)];
  114. #define MAX_RESOURCES 256
  115. #define RESOURCE_LABEL_SIZE 16
  116. struct str_ident {
  117. char name[RESOURCE_LABEL_SIZE];
  118. } *str_ident;
  119. #ifdef CONFIG_PM
  120. static unsigned short wakeup_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
  121. static unsigned char wakeup_flags_map[MAX_BLACKFIN_GPIOS];
  122. static struct gpio_port_s gpio_bank_saved[gpio_bank(MAX_BLACKFIN_GPIOS)];
  123. #ifdef BF533_FAMILY
  124. static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB};
  125. #endif
  126. #ifdef BF537_FAMILY
  127. static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB, IRQ_PORTG_INTB, IRQ_MAC_TX};
  128. #endif
  129. #ifdef BF561_FAMILY
  130. static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB};
  131. #endif
  132. #endif /* CONFIG_PM */
  133. inline int check_gpio(unsigned short gpio)
  134. {
  135. if (gpio >= MAX_BLACKFIN_GPIOS)
  136. return -EINVAL;
  137. return 0;
  138. }
  139. static void set_label(unsigned short ident, const char *label)
  140. {
  141. if (label && str_ident) {
  142. strncpy(str_ident[ident].name, label,
  143. RESOURCE_LABEL_SIZE);
  144. str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
  145. }
  146. }
  147. static char *get_label(unsigned short ident)
  148. {
  149. if (!str_ident)
  150. return "UNKNOWN";
  151. return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
  152. }
  153. static int cmp_label(unsigned short ident, const char *label)
  154. {
  155. if (label && str_ident)
  156. return strncmp(str_ident[ident].name,
  157. label, strlen(label));
  158. else
  159. return -EINVAL;
  160. }
  161. #ifdef BF537_FAMILY
  162. static void port_setup(unsigned short gpio, unsigned short usage)
  163. {
  164. if (!check_gpio(gpio)) {
  165. if (usage == GPIO_USAGE) {
  166. *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
  167. } else
  168. *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
  169. SSYNC();
  170. }
  171. }
  172. #else
  173. # define port_setup(...) do { } while (0)
  174. #endif
  175. #ifdef BF537_FAMILY
  176. static struct {
  177. unsigned short res;
  178. unsigned short offset;
  179. } port_mux_lut[] = {
  180. {.res = P_PPI0_D13, .offset = 11},
  181. {.res = P_PPI0_D14, .offset = 11},
  182. {.res = P_PPI0_D15, .offset = 11},
  183. {.res = P_SPORT1_TFS, .offset = 11},
  184. {.res = P_SPORT1_TSCLK, .offset = 11},
  185. {.res = P_SPORT1_DTPRI, .offset = 11},
  186. {.res = P_PPI0_D10, .offset = 10},
  187. {.res = P_PPI0_D11, .offset = 10},
  188. {.res = P_PPI0_D12, .offset = 10},
  189. {.res = P_SPORT1_RSCLK, .offset = 10},
  190. {.res = P_SPORT1_RFS, .offset = 10},
  191. {.res = P_SPORT1_DRPRI, .offset = 10},
  192. {.res = P_PPI0_D8, .offset = 9},
  193. {.res = P_PPI0_D9, .offset = 9},
  194. {.res = P_SPORT1_DRSEC, .offset = 9},
  195. {.res = P_SPORT1_DTSEC, .offset = 9},
  196. {.res = P_TMR2, .offset = 8},
  197. {.res = P_PPI0_FS3, .offset = 8},
  198. {.res = P_TMR3, .offset = 7},
  199. {.res = P_SPI0_SSEL4, .offset = 7},
  200. {.res = P_TMR4, .offset = 6},
  201. {.res = P_SPI0_SSEL5, .offset = 6},
  202. {.res = P_TMR5, .offset = 5},
  203. {.res = P_SPI0_SSEL6, .offset = 5},
  204. {.res = P_UART1_RX, .offset = 4},
  205. {.res = P_UART1_TX, .offset = 4},
  206. {.res = P_TMR6, .offset = 4},
  207. {.res = P_TMR7, .offset = 4},
  208. {.res = P_UART0_RX, .offset = 3},
  209. {.res = P_UART0_TX, .offset = 3},
  210. {.res = P_DMAR0, .offset = 3},
  211. {.res = P_DMAR1, .offset = 3},
  212. {.res = P_SPORT0_DTSEC, .offset = 1},
  213. {.res = P_SPORT0_DRSEC, .offset = 1},
  214. {.res = P_CAN0_RX, .offset = 1},
  215. {.res = P_CAN0_TX, .offset = 1},
  216. {.res = P_SPI0_SSEL7, .offset = 1},
  217. {.res = P_SPORT0_TFS, .offset = 0},
  218. {.res = P_SPORT0_DTPRI, .offset = 0},
  219. {.res = P_SPI0_SSEL2, .offset = 0},
  220. {.res = P_SPI0_SSEL3, .offset = 0},
  221. };
  222. static void portmux_setup(unsigned short per, unsigned short function)
  223. {
  224. u16 y, offset, muxreg;
  225. for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
  226. if (port_mux_lut[y].res == per) {
  227. /* SET PORTMUX REG */
  228. offset = port_mux_lut[y].offset;
  229. muxreg = bfin_read_PORT_MUX();
  230. if (offset != 1) {
  231. muxreg &= ~(1 << offset);
  232. } else {
  233. muxreg &= ~(3 << 1);
  234. }
  235. muxreg |= (function << offset);
  236. bfin_write_PORT_MUX(muxreg);
  237. }
  238. }
  239. }
  240. #else
  241. # define portmux_setup(...) do { } while (0)
  242. #endif
  243. static void default_gpio(unsigned short gpio)
  244. {
  245. unsigned short bank, bitmask;
  246. bank = gpio_bank(gpio);
  247. bitmask = gpio_bit(gpio);
  248. gpio_bankb[bank]->maska_clear = bitmask;
  249. gpio_bankb[bank]->maskb_clear = bitmask;
  250. SSYNC();
  251. gpio_bankb[bank]->inen &= ~bitmask;
  252. gpio_bankb[bank]->dir &= ~bitmask;
  253. gpio_bankb[bank]->polar &= ~bitmask;
  254. gpio_bankb[bank]->both &= ~bitmask;
  255. gpio_bankb[bank]->edge &= ~bitmask;
  256. }
  257. static int __init bfin_gpio_init(void)
  258. {
  259. str_ident = kcalloc(MAX_RESOURCES,
  260. sizeof(struct str_ident), GFP_KERNEL);
  261. if (str_ident == NULL)
  262. return -ENOMEM;
  263. memset(str_ident, 0, MAX_RESOURCES * sizeof(struct str_ident));
  264. printk(KERN_INFO "Blackfin GPIO Controller\n");
  265. return 0;
  266. }
  267. arch_initcall(bfin_gpio_init);
  268. /***********************************************************
  269. *
  270. * FUNCTIONS: Blackfin General Purpose Ports Access Functions
  271. *
  272. * INPUTS/OUTPUTS:
  273. * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
  274. *
  275. *
  276. * DESCRIPTION: These functions abstract direct register access
  277. * to Blackfin processor General Purpose
  278. * Ports Regsiters
  279. *
  280. * CAUTION: These functions do not belong to the GPIO Driver API
  281. *************************************************************
  282. * MODIFICATION HISTORY :
  283. **************************************************************/
  284. /* Set a specific bit */
  285. #define SET_GPIO(name) \
  286. void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \
  287. { \
  288. unsigned long flags; \
  289. BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
  290. local_irq_save(flags); \
  291. if (arg) \
  292. gpio_bankb[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
  293. else \
  294. gpio_bankb[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
  295. local_irq_restore(flags); \
  296. } \
  297. EXPORT_SYMBOL(set_gpio_ ## name);
  298. SET_GPIO(dir)
  299. SET_GPIO(inen)
  300. SET_GPIO(polar)
  301. SET_GPIO(edge)
  302. SET_GPIO(both)
  303. #define SET_GPIO_SC(name) \
  304. void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \
  305. { \
  306. BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
  307. if (arg) \
  308. gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
  309. else \
  310. gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
  311. } \
  312. EXPORT_SYMBOL(set_gpio_ ## name);
  313. SET_GPIO_SC(maska)
  314. SET_GPIO_SC(maskb)
  315. #if ANOMALY_05000311
  316. void set_gpio_data(unsigned short gpio, unsigned short arg)
  317. {
  318. unsigned long flags;
  319. BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
  320. local_irq_save(flags);
  321. if (arg)
  322. gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
  323. else
  324. gpio_bankb[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
  325. bfin_read_CHIPID();
  326. local_irq_restore(flags);
  327. }
  328. EXPORT_SYMBOL(set_gpio_data);
  329. #else
  330. SET_GPIO_SC(data)
  331. #endif
  332. #if ANOMALY_05000311
  333. void set_gpio_toggle(unsigned short gpio)
  334. {
  335. unsigned long flags;
  336. BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
  337. local_irq_save(flags);
  338. gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
  339. bfin_read_CHIPID();
  340. local_irq_restore(flags);
  341. }
  342. #else
  343. void set_gpio_toggle(unsigned short gpio)
  344. {
  345. BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
  346. gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
  347. }
  348. #endif
  349. EXPORT_SYMBOL(set_gpio_toggle);
  350. /*Set current PORT date (16-bit word)*/
  351. #define SET_GPIO_P(name) \
  352. void set_gpiop_ ## name(unsigned short gpio, unsigned short arg) \
  353. { \
  354. gpio_bankb[gpio_bank(gpio)]->name = arg; \
  355. } \
  356. EXPORT_SYMBOL(set_gpiop_ ## name);
  357. SET_GPIO_P(dir)
  358. SET_GPIO_P(inen)
  359. SET_GPIO_P(polar)
  360. SET_GPIO_P(edge)
  361. SET_GPIO_P(both)
  362. SET_GPIO_P(maska)
  363. SET_GPIO_P(maskb)
  364. #if ANOMALY_05000311
  365. void set_gpiop_data(unsigned short gpio, unsigned short arg)
  366. {
  367. unsigned long flags;
  368. local_irq_save(flags);
  369. gpio_bankb[gpio_bank(gpio)]->data = arg;
  370. bfin_read_CHIPID();
  371. local_irq_restore(flags);
  372. }
  373. EXPORT_SYMBOL(set_gpiop_data);
  374. #else
  375. SET_GPIO_P(data)
  376. #endif
  377. /* Get a specific bit */
  378. #define GET_GPIO(name) \
  379. unsigned short get_gpio_ ## name(unsigned short gpio) \
  380. { \
  381. return (0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio))); \
  382. } \
  383. EXPORT_SYMBOL(get_gpio_ ## name);
  384. GET_GPIO(dir)
  385. GET_GPIO(inen)
  386. GET_GPIO(polar)
  387. GET_GPIO(edge)
  388. GET_GPIO(both)
  389. GET_GPIO(maska)
  390. GET_GPIO(maskb)
  391. #if ANOMALY_05000311
  392. unsigned short get_gpio_data(unsigned short gpio)
  393. {
  394. unsigned long flags;
  395. unsigned short ret;
  396. BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
  397. local_irq_save(flags);
  398. ret = 0x01 & (gpio_bankb[gpio_bank(gpio)]->data >> gpio_sub_n(gpio));
  399. bfin_read_CHIPID();
  400. local_irq_restore(flags);
  401. return ret;
  402. }
  403. EXPORT_SYMBOL(get_gpio_data);
  404. #else
  405. GET_GPIO(data)
  406. #endif
  407. /*Get current PORT date (16-bit word)*/
  408. #define GET_GPIO_P(name) \
  409. unsigned short get_gpiop_ ## name(unsigned short gpio) \
  410. { \
  411. return (gpio_bankb[gpio_bank(gpio)]->name);\
  412. } \
  413. EXPORT_SYMBOL(get_gpiop_ ## name);
  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. #if ANOMALY_05000311
  422. unsigned short get_gpiop_data(unsigned short gpio)
  423. {
  424. unsigned long flags;
  425. unsigned short ret;
  426. local_irq_save(flags);
  427. ret = gpio_bankb[gpio_bank(gpio)]->data;
  428. bfin_read_CHIPID();
  429. local_irq_restore(flags);
  430. return ret;
  431. }
  432. EXPORT_SYMBOL(get_gpiop_data);
  433. #else
  434. GET_GPIO_P(data)
  435. #endif
  436. #ifdef CONFIG_PM
  437. /***********************************************************
  438. *
  439. * FUNCTIONS: Blackfin PM Setup API
  440. *
  441. * INPUTS/OUTPUTS:
  442. * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
  443. * type -
  444. * PM_WAKE_RISING
  445. * PM_WAKE_FALLING
  446. * PM_WAKE_HIGH
  447. * PM_WAKE_LOW
  448. * PM_WAKE_BOTH_EDGES
  449. *
  450. * DESCRIPTION: Blackfin PM Driver API
  451. *
  452. * CAUTION:
  453. *************************************************************
  454. * MODIFICATION HISTORY :
  455. **************************************************************/
  456. int gpio_pm_wakeup_request(unsigned short gpio, unsigned char type)
  457. {
  458. unsigned long flags;
  459. if ((check_gpio(gpio) < 0) || !type)
  460. return -EINVAL;
  461. local_irq_save(flags);
  462. wakeup_map[gpio_bank(gpio)] |= gpio_bit(gpio);
  463. wakeup_flags_map[gpio] = type;
  464. local_irq_restore(flags);
  465. return 0;
  466. }
  467. EXPORT_SYMBOL(gpio_pm_wakeup_request);
  468. void gpio_pm_wakeup_free(unsigned short gpio)
  469. {
  470. unsigned long flags;
  471. if (check_gpio(gpio) < 0)
  472. return;
  473. local_irq_save(flags);
  474. wakeup_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
  475. local_irq_restore(flags);
  476. }
  477. EXPORT_SYMBOL(gpio_pm_wakeup_free);
  478. static int bfin_gpio_wakeup_type(unsigned short gpio, unsigned char type)
  479. {
  480. port_setup(gpio, GPIO_USAGE);
  481. set_gpio_dir(gpio, 0);
  482. set_gpio_inen(gpio, 1);
  483. if (type & (PM_WAKE_RISING | PM_WAKE_FALLING))
  484. set_gpio_edge(gpio, 1);
  485. else
  486. set_gpio_edge(gpio, 0);
  487. if ((type & (PM_WAKE_BOTH_EDGES)) == (PM_WAKE_BOTH_EDGES))
  488. set_gpio_both(gpio, 1);
  489. else
  490. set_gpio_both(gpio, 0);
  491. if ((type & (PM_WAKE_FALLING | PM_WAKE_LOW)))
  492. set_gpio_polar(gpio, 1);
  493. else
  494. set_gpio_polar(gpio, 0);
  495. SSYNC();
  496. return 0;
  497. }
  498. u32 gpio_pm_setup(void)
  499. {
  500. u32 sic_iwr = 0;
  501. u16 bank, mask, i, gpio;
  502. for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
  503. mask = wakeup_map[gpio_bank(i)];
  504. bank = gpio_bank(i);
  505. gpio_bank_saved[bank].maskb = gpio_bankb[bank]->maskb;
  506. gpio_bankb[bank]->maskb = 0;
  507. if (mask) {
  508. #ifdef BF537_FAMILY
  509. gpio_bank_saved[bank].fer = *port_fer[bank];
  510. #endif
  511. gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
  512. gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
  513. gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
  514. gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
  515. gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
  516. gpio_bank_saved[bank].reserved =
  517. reserved_gpio_map[bank];
  518. gpio = i;
  519. while (mask) {
  520. if (mask & 1) {
  521. reserved_gpio_map[gpio_bank(gpio)] |=
  522. gpio_bit(gpio);
  523. bfin_gpio_wakeup_type(gpio,
  524. wakeup_flags_map[gpio]);
  525. set_gpio_data(gpio, 0); /*Clear*/
  526. }
  527. gpio++;
  528. mask >>= 1;
  529. }
  530. sic_iwr |= 1 <<
  531. (sic_iwr_irqs[bank] - (IRQ_CORETMR + 1));
  532. gpio_bankb[bank]->maskb_set = wakeup_map[gpio_bank(i)];
  533. }
  534. }
  535. if (sic_iwr)
  536. return sic_iwr;
  537. else
  538. return IWR_ENABLE_ALL;
  539. }
  540. void gpio_pm_restore(void)
  541. {
  542. u16 bank, mask, i;
  543. for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
  544. mask = wakeup_map[gpio_bank(i)];
  545. bank = gpio_bank(i);
  546. if (mask) {
  547. #ifdef BF537_FAMILY
  548. *port_fer[bank] = gpio_bank_saved[bank].fer;
  549. #endif
  550. gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
  551. gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
  552. gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
  553. gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
  554. gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
  555. reserved_gpio_map[bank] =
  556. gpio_bank_saved[bank].reserved;
  557. }
  558. gpio_bankb[bank]->maskb = gpio_bank_saved[bank].maskb;
  559. }
  560. }
  561. #endif
  562. int peripheral_request(unsigned short per, const char *label)
  563. {
  564. unsigned long flags;
  565. unsigned short ident = P_IDENT(per);
  566. /*
  567. * Don't cares are pins with only one dedicated function
  568. */
  569. if (per & P_DONTCARE)
  570. return 0;
  571. if (!(per & P_DEFINED))
  572. return -ENODEV;
  573. local_irq_save(flags);
  574. if (!check_gpio(ident)) {
  575. if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
  576. printk(KERN_ERR
  577. "%s: Peripheral %d is already reserved as GPIO by %s !\n",
  578. __FUNCTION__, ident, get_label(ident));
  579. dump_stack();
  580. local_irq_restore(flags);
  581. return -EBUSY;
  582. }
  583. }
  584. if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
  585. /*
  586. * Pin functions like AMC address strobes my
  587. * be requested and used by several drivers
  588. */
  589. if (!(per & P_MAYSHARE)) {
  590. /*
  591. * Allow that the identical pin function can
  592. * be requested from the same driver twice
  593. */
  594. if (cmp_label(ident, label) == 0)
  595. goto anyway;
  596. printk(KERN_ERR
  597. "%s: Peripheral %d function %d is already"
  598. " reserved by %s !\n",
  599. __FUNCTION__, ident, P_FUNCT2MUX(per),
  600. get_label(ident));
  601. dump_stack();
  602. local_irq_restore(flags);
  603. return -EBUSY;
  604. }
  605. }
  606. anyway:
  607. portmux_setup(per, P_FUNCT2MUX(per));
  608. port_setup(ident, PERIPHERAL_USAGE);
  609. reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
  610. local_irq_restore(flags);
  611. set_label(ident, label);
  612. return 0;
  613. }
  614. EXPORT_SYMBOL(peripheral_request);
  615. int peripheral_request_list(unsigned short per[], const char *label)
  616. {
  617. u16 cnt;
  618. int ret;
  619. for (cnt = 0; per[cnt] != 0; cnt++) {
  620. ret = peripheral_request(per[cnt], label);
  621. if (ret < 0) {
  622. for ( ; cnt > 0; cnt--) {
  623. peripheral_free(per[cnt - 1]);
  624. }
  625. return ret;
  626. }
  627. }
  628. return 0;
  629. }
  630. EXPORT_SYMBOL(peripheral_request_list);
  631. void peripheral_free(unsigned short per)
  632. {
  633. unsigned long flags;
  634. unsigned short ident = P_IDENT(per);
  635. if (per & P_DONTCARE)
  636. return;
  637. if (!(per & P_DEFINED))
  638. return;
  639. if (check_gpio(ident) < 0)
  640. return;
  641. local_irq_save(flags);
  642. if (unlikely(!(reserved_peri_map[gpio_bank(ident)]
  643. & gpio_bit(ident)))) {
  644. local_irq_restore(flags);
  645. return;
  646. }
  647. if (!(per & P_MAYSHARE)) {
  648. port_setup(ident, GPIO_USAGE);
  649. }
  650. reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
  651. local_irq_restore(flags);
  652. }
  653. EXPORT_SYMBOL(peripheral_free);
  654. void peripheral_free_list(unsigned short per[])
  655. {
  656. u16 cnt;
  657. for (cnt = 0; per[cnt] != 0; cnt++) {
  658. peripheral_free(per[cnt]);
  659. }
  660. }
  661. EXPORT_SYMBOL(peripheral_free_list);
  662. /***********************************************************
  663. *
  664. * FUNCTIONS: Blackfin GPIO Driver
  665. *
  666. * INPUTS/OUTPUTS:
  667. * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
  668. *
  669. *
  670. * DESCRIPTION: Blackfin GPIO Driver API
  671. *
  672. * CAUTION:
  673. *************************************************************
  674. * MODIFICATION HISTORY :
  675. **************************************************************/
  676. int gpio_request(unsigned short gpio, const char *label)
  677. {
  678. unsigned long flags;
  679. if (check_gpio(gpio) < 0)
  680. return -EINVAL;
  681. local_irq_save(flags);
  682. if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
  683. printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved!\n", gpio);
  684. dump_stack();
  685. local_irq_restore(flags);
  686. return -EBUSY;
  687. }
  688. reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
  689. local_irq_restore(flags);
  690. port_setup(gpio, GPIO_USAGE);
  691. return 0;
  692. }
  693. EXPORT_SYMBOL(gpio_request);
  694. void gpio_free(unsigned short gpio)
  695. {
  696. unsigned long flags;
  697. if (check_gpio(gpio) < 0)
  698. return;
  699. local_irq_save(flags);
  700. if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
  701. printk(KERN_ERR "bfin-gpio: GPIO %d wasn't reserved!\n", gpio);
  702. dump_stack();
  703. local_irq_restore(flags);
  704. return;
  705. }
  706. default_gpio(gpio);
  707. reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
  708. local_irq_restore(flags);
  709. }
  710. EXPORT_SYMBOL(gpio_free);
  711. void gpio_direction_input(unsigned short gpio)
  712. {
  713. unsigned long flags;
  714. BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
  715. local_irq_save(flags);
  716. gpio_bankb[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
  717. gpio_bankb[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
  718. local_irq_restore(flags);
  719. }
  720. EXPORT_SYMBOL(gpio_direction_input);
  721. void gpio_direction_output(unsigned short gpio)
  722. {
  723. unsigned long flags;
  724. BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
  725. local_irq_save(flags);
  726. gpio_bankb[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
  727. gpio_bankb[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
  728. local_irq_restore(flags);
  729. }
  730. EXPORT_SYMBOL(gpio_direction_output);
  731. /* If we are booting from SPI and our board lacks a strong enough pull up,
  732. * the core can reset and execute the bootrom faster than the resistor can
  733. * pull the signal logically high. To work around this (common) error in
  734. * board design, we explicitly set the pin back to GPIO mode, force /CS
  735. * high, and wait for the electrons to do their thing.
  736. *
  737. * This function only makes sense to be called from reset code, but it
  738. * lives here as we need to force all the GPIO states w/out going through
  739. * BUG() checks and such.
  740. */
  741. void bfin_gpio_reset_spi0_ssel1(void)
  742. {
  743. port_setup(P_SPI0_SSEL1, GPIO_USAGE);
  744. gpio_bankb[gpio_bank(P_SPI0_SSEL1)]->data_set = gpio_bit(P_SPI0_SSEL1);
  745. udelay(1);
  746. }