spi_mpc8xxx.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. /*
  2. * MPC8xxx SPI controller driver.
  3. *
  4. * Maintainer: Kumar Gala
  5. *
  6. * Copyright (C) 2006 Polycom, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/bug.h>
  18. #include <linux/errno.h>
  19. #include <linux/err.h>
  20. #include <linux/io.h>
  21. #include <linux/completion.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/delay.h>
  24. #include <linux/irq.h>
  25. #include <linux/device.h>
  26. #include <linux/spi/spi.h>
  27. #include <linux/spi/spi_bitbang.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/fsl_devices.h>
  30. #include <linux/of.h>
  31. #include <linux/of_platform.h>
  32. #include <linux/gpio.h>
  33. #include <linux/of_gpio.h>
  34. #include <linux/of_spi.h>
  35. #include <sysdev/fsl_soc.h>
  36. #include <asm/irq.h>
  37. /* SPI Controller registers */
  38. struct mpc8xxx_spi_reg {
  39. u8 res1[0x20];
  40. __be32 mode;
  41. __be32 event;
  42. __be32 mask;
  43. __be32 command;
  44. __be32 transmit;
  45. __be32 receive;
  46. };
  47. /* SPI Controller mode register definitions */
  48. #define SPMODE_LOOP (1 << 30)
  49. #define SPMODE_CI_INACTIVEHIGH (1 << 29)
  50. #define SPMODE_CP_BEGIN_EDGECLK (1 << 28)
  51. #define SPMODE_DIV16 (1 << 27)
  52. #define SPMODE_REV (1 << 26)
  53. #define SPMODE_MS (1 << 25)
  54. #define SPMODE_ENABLE (1 << 24)
  55. #define SPMODE_LEN(x) ((x) << 20)
  56. #define SPMODE_PM(x) ((x) << 16)
  57. #define SPMODE_OP (1 << 14)
  58. #define SPMODE_CG(x) ((x) << 7)
  59. /*
  60. * Default for SPI Mode:
  61. * SPI MODE 0 (inactive low, phase middle, MSB, 8-bit length, slow clk
  62. */
  63. #define SPMODE_INIT_VAL (SPMODE_CI_INACTIVEHIGH | SPMODE_DIV16 | SPMODE_REV | \
  64. SPMODE_MS | SPMODE_LEN(7) | SPMODE_PM(0xf))
  65. /* SPIE register values */
  66. #define SPIE_NE 0x00000200 /* Not empty */
  67. #define SPIE_NF 0x00000100 /* Not full */
  68. /* SPIM register values */
  69. #define SPIM_NE 0x00000200 /* Not empty */
  70. #define SPIM_NF 0x00000100 /* Not full */
  71. /* SPI Controller driver's private data. */
  72. struct mpc8xxx_spi {
  73. struct mpc8xxx_spi_reg __iomem *base;
  74. /* rx & tx bufs from the spi_transfer */
  75. const void *tx;
  76. void *rx;
  77. /* functions to deal with different sized buffers */
  78. void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
  79. u32(*get_tx) (struct mpc8xxx_spi *);
  80. unsigned int count;
  81. unsigned int irq;
  82. unsigned nsecs; /* (clock cycle time)/2 */
  83. u32 spibrg; /* SPIBRG input clock */
  84. u32 rx_shift; /* RX data reg shift when in qe mode */
  85. u32 tx_shift; /* TX data reg shift when in qe mode */
  86. unsigned int flags;
  87. #define SPI_QE_CPU_MODE (1 << 0) /* QE CPU ("PIO") mode */
  88. struct workqueue_struct *workqueue;
  89. struct work_struct work;
  90. struct list_head queue;
  91. spinlock_t lock;
  92. struct completion done;
  93. };
  94. struct spi_mpc8xxx_cs {
  95. /* functions to deal with different sized buffers */
  96. void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
  97. u32 (*get_tx) (struct mpc8xxx_spi *);
  98. u32 rx_shift; /* RX data reg shift when in qe mode */
  99. u32 tx_shift; /* TX data reg shift when in qe mode */
  100. u32 hw_mode; /* Holds HW mode register settings */
  101. };
  102. static inline void mpc8xxx_spi_write_reg(__be32 __iomem *reg, u32 val)
  103. {
  104. out_be32(reg, val);
  105. }
  106. static inline u32 mpc8xxx_spi_read_reg(__be32 __iomem *reg)
  107. {
  108. return in_be32(reg);
  109. }
  110. #define MPC83XX_SPI_RX_BUF(type) \
  111. static \
  112. void mpc8xxx_spi_rx_buf_##type(u32 data, struct mpc8xxx_spi *mpc8xxx_spi) \
  113. { \
  114. type *rx = mpc8xxx_spi->rx; \
  115. *rx++ = (type)(data >> mpc8xxx_spi->rx_shift); \
  116. mpc8xxx_spi->rx = rx; \
  117. }
  118. #define MPC83XX_SPI_TX_BUF(type) \
  119. static \
  120. u32 mpc8xxx_spi_tx_buf_##type(struct mpc8xxx_spi *mpc8xxx_spi) \
  121. { \
  122. u32 data; \
  123. const type *tx = mpc8xxx_spi->tx; \
  124. if (!tx) \
  125. return 0; \
  126. data = *tx++ << mpc8xxx_spi->tx_shift; \
  127. mpc8xxx_spi->tx = tx; \
  128. return data; \
  129. }
  130. MPC83XX_SPI_RX_BUF(u8)
  131. MPC83XX_SPI_RX_BUF(u16)
  132. MPC83XX_SPI_RX_BUF(u32)
  133. MPC83XX_SPI_TX_BUF(u8)
  134. MPC83XX_SPI_TX_BUF(u16)
  135. MPC83XX_SPI_TX_BUF(u32)
  136. static void mpc8xxx_spi_change_mode(struct spi_device *spi)
  137. {
  138. struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
  139. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  140. __be32 __iomem *mode = &mspi->base->mode;
  141. unsigned long flags;
  142. if (cs->hw_mode == mpc8xxx_spi_read_reg(mode))
  143. return;
  144. /* Turn off IRQs locally to minimize time that SPI is disabled. */
  145. local_irq_save(flags);
  146. /* Turn off SPI unit prior changing mode */
  147. mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE);
  148. mpc8xxx_spi_write_reg(mode, cs->hw_mode);
  149. local_irq_restore(flags);
  150. }
  151. static void mpc8xxx_spi_chipselect(struct spi_device *spi, int value)
  152. {
  153. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  154. struct fsl_spi_platform_data *pdata = spi->dev.parent->platform_data;
  155. bool pol = spi->mode & SPI_CS_HIGH;
  156. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  157. if (value == BITBANG_CS_INACTIVE) {
  158. if (pdata->cs_control)
  159. pdata->cs_control(spi, !pol);
  160. }
  161. if (value == BITBANG_CS_ACTIVE) {
  162. mpc8xxx_spi->rx_shift = cs->rx_shift;
  163. mpc8xxx_spi->tx_shift = cs->tx_shift;
  164. mpc8xxx_spi->get_rx = cs->get_rx;
  165. mpc8xxx_spi->get_tx = cs->get_tx;
  166. mpc8xxx_spi_change_mode(spi);
  167. if (pdata->cs_control)
  168. pdata->cs_control(spi, pol);
  169. }
  170. }
  171. static
  172. int mpc8xxx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  173. {
  174. struct mpc8xxx_spi *mpc8xxx_spi;
  175. u8 bits_per_word, pm;
  176. u32 hz;
  177. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  178. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  179. if (t) {
  180. bits_per_word = t->bits_per_word;
  181. hz = t->speed_hz;
  182. } else {
  183. bits_per_word = 0;
  184. hz = 0;
  185. }
  186. /* spi_transfer level calls that work per-word */
  187. if (!bits_per_word)
  188. bits_per_word = spi->bits_per_word;
  189. /* Make sure its a bit width we support [4..16, 32] */
  190. if ((bits_per_word < 4)
  191. || ((bits_per_word > 16) && (bits_per_word != 32)))
  192. return -EINVAL;
  193. if (!hz)
  194. hz = spi->max_speed_hz;
  195. cs->rx_shift = 0;
  196. cs->tx_shift = 0;
  197. if (bits_per_word <= 8) {
  198. cs->get_rx = mpc8xxx_spi_rx_buf_u8;
  199. cs->get_tx = mpc8xxx_spi_tx_buf_u8;
  200. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  201. cs->rx_shift = 16;
  202. cs->tx_shift = 24;
  203. }
  204. } else if (bits_per_word <= 16) {
  205. cs->get_rx = mpc8xxx_spi_rx_buf_u16;
  206. cs->get_tx = mpc8xxx_spi_tx_buf_u16;
  207. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  208. cs->rx_shift = 16;
  209. cs->tx_shift = 16;
  210. }
  211. } else if (bits_per_word <= 32) {
  212. cs->get_rx = mpc8xxx_spi_rx_buf_u32;
  213. cs->get_tx = mpc8xxx_spi_tx_buf_u32;
  214. } else
  215. return -EINVAL;
  216. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE &&
  217. spi->mode & SPI_LSB_FIRST) {
  218. cs->tx_shift = 0;
  219. if (bits_per_word <= 8)
  220. cs->rx_shift = 8;
  221. else
  222. cs->rx_shift = 0;
  223. }
  224. mpc8xxx_spi->rx_shift = cs->rx_shift;
  225. mpc8xxx_spi->tx_shift = cs->tx_shift;
  226. mpc8xxx_spi->get_rx = cs->get_rx;
  227. mpc8xxx_spi->get_tx = cs->get_tx;
  228. if (bits_per_word == 32)
  229. bits_per_word = 0;
  230. else
  231. bits_per_word = bits_per_word - 1;
  232. /* mask out bits we are going to set */
  233. cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
  234. | SPMODE_PM(0xF));
  235. cs->hw_mode |= SPMODE_LEN(bits_per_word);
  236. if ((mpc8xxx_spi->spibrg / hz) > 64) {
  237. cs->hw_mode |= SPMODE_DIV16;
  238. pm = mpc8xxx_spi->spibrg / (hz * 64);
  239. WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. "
  240. "Will use %d Hz instead.\n", dev_name(&spi->dev),
  241. hz, mpc8xxx_spi->spibrg / 1024);
  242. if (pm > 16)
  243. pm = 16;
  244. } else
  245. pm = mpc8xxx_spi->spibrg / (hz * 4);
  246. if (pm)
  247. pm--;
  248. cs->hw_mode |= SPMODE_PM(pm);
  249. mpc8xxx_spi_change_mode(spi);
  250. return 0;
  251. }
  252. static int mpc8xxx_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
  253. {
  254. struct mpc8xxx_spi *mpc8xxx_spi;
  255. u32 word, len, bits_per_word;
  256. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  257. mpc8xxx_spi->tx = t->tx_buf;
  258. mpc8xxx_spi->rx = t->rx_buf;
  259. bits_per_word = spi->bits_per_word;
  260. if (t->bits_per_word)
  261. bits_per_word = t->bits_per_word;
  262. len = t->len;
  263. if (bits_per_word > 8) {
  264. /* invalid length? */
  265. if (len & 1)
  266. return -EINVAL;
  267. len /= 2;
  268. }
  269. if (bits_per_word > 16) {
  270. /* invalid length? */
  271. if (len & 1)
  272. return -EINVAL;
  273. len /= 2;
  274. }
  275. mpc8xxx_spi->count = len;
  276. INIT_COMPLETION(mpc8xxx_spi->done);
  277. /* enable rx ints */
  278. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, SPIM_NE);
  279. /* transmit word */
  280. word = mpc8xxx_spi->get_tx(mpc8xxx_spi);
  281. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->transmit, word);
  282. wait_for_completion(&mpc8xxx_spi->done);
  283. /* disable rx ints */
  284. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
  285. return mpc8xxx_spi->count;
  286. }
  287. static void mpc8xxx_spi_do_one_msg(struct spi_message *m)
  288. {
  289. struct spi_device *spi = m->spi;
  290. struct spi_transfer *t;
  291. unsigned int cs_change;
  292. const int nsecs = 50;
  293. int status;
  294. cs_change = 1;
  295. status = 0;
  296. list_for_each_entry(t, &m->transfers, transfer_list) {
  297. if (t->bits_per_word || t->speed_hz) {
  298. /* Don't allow changes if CS is active */
  299. status = -EINVAL;
  300. if (cs_change)
  301. status = mpc8xxx_spi_setup_transfer(spi, t);
  302. if (status < 0)
  303. break;
  304. }
  305. if (cs_change) {
  306. mpc8xxx_spi_chipselect(spi, BITBANG_CS_ACTIVE);
  307. ndelay(nsecs);
  308. }
  309. cs_change = t->cs_change;
  310. if (t->len)
  311. status = mpc8xxx_spi_bufs(spi, t);
  312. if (status) {
  313. status = -EMSGSIZE;
  314. break;
  315. }
  316. m->actual_length += t->len;
  317. if (t->delay_usecs)
  318. udelay(t->delay_usecs);
  319. if (cs_change) {
  320. ndelay(nsecs);
  321. mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  322. ndelay(nsecs);
  323. }
  324. }
  325. m->status = status;
  326. m->complete(m->context);
  327. if (status || !cs_change) {
  328. ndelay(nsecs);
  329. mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  330. }
  331. mpc8xxx_spi_setup_transfer(spi, NULL);
  332. }
  333. static void mpc8xxx_spi_work(struct work_struct *work)
  334. {
  335. struct mpc8xxx_spi *mpc8xxx_spi = container_of(work, struct mpc8xxx_spi,
  336. work);
  337. spin_lock_irq(&mpc8xxx_spi->lock);
  338. while (!list_empty(&mpc8xxx_spi->queue)) {
  339. struct spi_message *m = container_of(mpc8xxx_spi->queue.next,
  340. struct spi_message, queue);
  341. list_del_init(&m->queue);
  342. spin_unlock_irq(&mpc8xxx_spi->lock);
  343. mpc8xxx_spi_do_one_msg(m);
  344. spin_lock_irq(&mpc8xxx_spi->lock);
  345. }
  346. spin_unlock_irq(&mpc8xxx_spi->lock);
  347. }
  348. static int mpc8xxx_spi_setup(struct spi_device *spi)
  349. {
  350. struct mpc8xxx_spi *mpc8xxx_spi;
  351. int retval;
  352. u32 hw_mode;
  353. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  354. if (!spi->max_speed_hz)
  355. return -EINVAL;
  356. if (!cs) {
  357. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  358. if (!cs)
  359. return -ENOMEM;
  360. spi->controller_state = cs;
  361. }
  362. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  363. hw_mode = cs->hw_mode; /* Save orginal settings */
  364. cs->hw_mode = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->mode);
  365. /* mask out bits we are going to set */
  366. cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
  367. | SPMODE_REV | SPMODE_LOOP);
  368. if (spi->mode & SPI_CPHA)
  369. cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
  370. if (spi->mode & SPI_CPOL)
  371. cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
  372. if (!(spi->mode & SPI_LSB_FIRST))
  373. cs->hw_mode |= SPMODE_REV;
  374. if (spi->mode & SPI_LOOP)
  375. cs->hw_mode |= SPMODE_LOOP;
  376. retval = mpc8xxx_spi_setup_transfer(spi, NULL);
  377. if (retval < 0) {
  378. cs->hw_mode = hw_mode; /* Restore settings */
  379. return retval;
  380. }
  381. return 0;
  382. }
  383. static irqreturn_t mpc8xxx_spi_irq(s32 irq, void *context_data)
  384. {
  385. struct mpc8xxx_spi *mpc8xxx_spi = context_data;
  386. u32 event;
  387. irqreturn_t ret = IRQ_NONE;
  388. /* Get interrupt events(tx/rx) */
  389. event = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->event);
  390. /* We need handle RX first */
  391. if (event & SPIE_NE) {
  392. u32 rx_data = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->receive);
  393. if (mpc8xxx_spi->rx)
  394. mpc8xxx_spi->get_rx(rx_data, mpc8xxx_spi);
  395. ret = IRQ_HANDLED;
  396. }
  397. if ((event & SPIE_NF) == 0)
  398. /* spin until TX is done */
  399. while (((event =
  400. mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->event)) &
  401. SPIE_NF) == 0)
  402. cpu_relax();
  403. mpc8xxx_spi->count -= 1;
  404. if (mpc8xxx_spi->count) {
  405. u32 word = mpc8xxx_spi->get_tx(mpc8xxx_spi);
  406. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->transmit, word);
  407. } else {
  408. complete(&mpc8xxx_spi->done);
  409. }
  410. /* Clear the events */
  411. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->event, event);
  412. return ret;
  413. }
  414. static int mpc8xxx_spi_transfer(struct spi_device *spi,
  415. struct spi_message *m)
  416. {
  417. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  418. unsigned long flags;
  419. m->actual_length = 0;
  420. m->status = -EINPROGRESS;
  421. spin_lock_irqsave(&mpc8xxx_spi->lock, flags);
  422. list_add_tail(&m->queue, &mpc8xxx_spi->queue);
  423. queue_work(mpc8xxx_spi->workqueue, &mpc8xxx_spi->work);
  424. spin_unlock_irqrestore(&mpc8xxx_spi->lock, flags);
  425. return 0;
  426. }
  427. static void mpc8xxx_spi_cleanup(struct spi_device *spi)
  428. {
  429. kfree(spi->controller_state);
  430. }
  431. static const char *mpc8xxx_spi_strmode(unsigned int flags)
  432. {
  433. if (flags & SPI_QE_CPU_MODE)
  434. return "QE CPU";
  435. return "CPU";
  436. }
  437. static struct spi_master * __devinit
  438. mpc8xxx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq)
  439. {
  440. struct fsl_spi_platform_data *pdata = dev->platform_data;
  441. struct spi_master *master;
  442. struct mpc8xxx_spi *mpc8xxx_spi;
  443. u32 regval;
  444. int ret = 0;
  445. master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
  446. if (master == NULL) {
  447. ret = -ENOMEM;
  448. goto err;
  449. }
  450. dev_set_drvdata(dev, master);
  451. /* the spi->mode bits understood by this driver: */
  452. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH
  453. | SPI_LSB_FIRST | SPI_LOOP;
  454. master->setup = mpc8xxx_spi_setup;
  455. master->transfer = mpc8xxx_spi_transfer;
  456. master->cleanup = mpc8xxx_spi_cleanup;
  457. mpc8xxx_spi = spi_master_get_devdata(master);
  458. mpc8xxx_spi->get_rx = mpc8xxx_spi_rx_buf_u8;
  459. mpc8xxx_spi->get_tx = mpc8xxx_spi_tx_buf_u8;
  460. mpc8xxx_spi->flags = pdata->flags;
  461. mpc8xxx_spi->spibrg = pdata->sysclk;
  462. mpc8xxx_spi->rx_shift = 0;
  463. mpc8xxx_spi->tx_shift = 0;
  464. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  465. mpc8xxx_spi->rx_shift = 16;
  466. mpc8xxx_spi->tx_shift = 24;
  467. }
  468. init_completion(&mpc8xxx_spi->done);
  469. mpc8xxx_spi->base = ioremap(mem->start, mem->end - mem->start + 1);
  470. if (mpc8xxx_spi->base == NULL) {
  471. ret = -ENOMEM;
  472. goto put_master;
  473. }
  474. mpc8xxx_spi->irq = irq;
  475. /* Register for SPI Interrupt */
  476. ret = request_irq(mpc8xxx_spi->irq, mpc8xxx_spi_irq,
  477. 0, "mpc8xxx_spi", mpc8xxx_spi);
  478. if (ret != 0)
  479. goto unmap_io;
  480. master->bus_num = pdata->bus_num;
  481. master->num_chipselect = pdata->max_chipselect;
  482. /* SPI controller initializations */
  483. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, 0);
  484. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
  485. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->command, 0);
  486. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->event, 0xffffffff);
  487. /* Enable SPI interface */
  488. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  489. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
  490. regval |= SPMODE_OP;
  491. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, regval);
  492. spin_lock_init(&mpc8xxx_spi->lock);
  493. init_completion(&mpc8xxx_spi->done);
  494. INIT_WORK(&mpc8xxx_spi->work, mpc8xxx_spi_work);
  495. INIT_LIST_HEAD(&mpc8xxx_spi->queue);
  496. mpc8xxx_spi->workqueue = create_singlethread_workqueue(
  497. dev_name(master->dev.parent));
  498. if (mpc8xxx_spi->workqueue == NULL) {
  499. ret = -EBUSY;
  500. goto free_irq;
  501. }
  502. ret = spi_register_master(master);
  503. if (ret < 0)
  504. goto unreg_master;
  505. dev_info(dev, "at 0x%p (irq = %d), %s mode\n", mpc8xxx_spi->base,
  506. mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
  507. return master;
  508. unreg_master:
  509. destroy_workqueue(mpc8xxx_spi->workqueue);
  510. free_irq:
  511. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  512. unmap_io:
  513. iounmap(mpc8xxx_spi->base);
  514. put_master:
  515. spi_master_put(master);
  516. err:
  517. return ERR_PTR(ret);
  518. }
  519. static int __devexit mpc8xxx_spi_remove(struct device *dev)
  520. {
  521. struct mpc8xxx_spi *mpc8xxx_spi;
  522. struct spi_master *master;
  523. master = dev_get_drvdata(dev);
  524. mpc8xxx_spi = spi_master_get_devdata(master);
  525. flush_workqueue(mpc8xxx_spi->workqueue);
  526. destroy_workqueue(mpc8xxx_spi->workqueue);
  527. spi_unregister_master(master);
  528. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  529. iounmap(mpc8xxx_spi->base);
  530. return 0;
  531. }
  532. struct mpc8xxx_spi_probe_info {
  533. struct fsl_spi_platform_data pdata;
  534. int *gpios;
  535. bool *alow_flags;
  536. };
  537. static struct mpc8xxx_spi_probe_info *
  538. to_of_pinfo(struct fsl_spi_platform_data *pdata)
  539. {
  540. return container_of(pdata, struct mpc8xxx_spi_probe_info, pdata);
  541. }
  542. static void mpc8xxx_spi_cs_control(struct spi_device *spi, bool on)
  543. {
  544. struct device *dev = spi->dev.parent;
  545. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
  546. u16 cs = spi->chip_select;
  547. int gpio = pinfo->gpios[cs];
  548. bool alow = pinfo->alow_flags[cs];
  549. gpio_set_value(gpio, on ^ alow);
  550. }
  551. static int of_mpc8xxx_spi_get_chipselects(struct device *dev)
  552. {
  553. struct device_node *np = dev_archdata_get_node(&dev->archdata);
  554. struct fsl_spi_platform_data *pdata = dev->platform_data;
  555. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  556. unsigned int ngpios;
  557. int i = 0;
  558. int ret;
  559. ngpios = of_gpio_count(np);
  560. if (!ngpios) {
  561. /*
  562. * SPI w/o chip-select line. One SPI device is still permitted
  563. * though.
  564. */
  565. pdata->max_chipselect = 1;
  566. return 0;
  567. }
  568. pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
  569. if (!pinfo->gpios)
  570. return -ENOMEM;
  571. memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
  572. pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
  573. GFP_KERNEL);
  574. if (!pinfo->alow_flags) {
  575. ret = -ENOMEM;
  576. goto err_alloc_flags;
  577. }
  578. for (; i < ngpios; i++) {
  579. int gpio;
  580. enum of_gpio_flags flags;
  581. gpio = of_get_gpio_flags(np, i, &flags);
  582. if (!gpio_is_valid(gpio)) {
  583. dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
  584. ret = gpio;
  585. goto err_loop;
  586. }
  587. ret = gpio_request(gpio, dev_name(dev));
  588. if (ret) {
  589. dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
  590. goto err_loop;
  591. }
  592. pinfo->gpios[i] = gpio;
  593. pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
  594. ret = gpio_direction_output(pinfo->gpios[i],
  595. pinfo->alow_flags[i]);
  596. if (ret) {
  597. dev_err(dev, "can't set output direction for gpio "
  598. "#%d: %d\n", i, ret);
  599. goto err_loop;
  600. }
  601. }
  602. pdata->max_chipselect = ngpios;
  603. pdata->cs_control = mpc8xxx_spi_cs_control;
  604. return 0;
  605. err_loop:
  606. while (i >= 0) {
  607. if (gpio_is_valid(pinfo->gpios[i]))
  608. gpio_free(pinfo->gpios[i]);
  609. i--;
  610. }
  611. kfree(pinfo->alow_flags);
  612. pinfo->alow_flags = NULL;
  613. err_alloc_flags:
  614. kfree(pinfo->gpios);
  615. pinfo->gpios = NULL;
  616. return ret;
  617. }
  618. static int of_mpc8xxx_spi_free_chipselects(struct device *dev)
  619. {
  620. struct fsl_spi_platform_data *pdata = dev->platform_data;
  621. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  622. int i;
  623. if (!pinfo->gpios)
  624. return 0;
  625. for (i = 0; i < pdata->max_chipselect; i++) {
  626. if (gpio_is_valid(pinfo->gpios[i]))
  627. gpio_free(pinfo->gpios[i]);
  628. }
  629. kfree(pinfo->gpios);
  630. kfree(pinfo->alow_flags);
  631. return 0;
  632. }
  633. static int __devinit of_mpc8xxx_spi_probe(struct of_device *ofdev,
  634. const struct of_device_id *ofid)
  635. {
  636. struct device *dev = &ofdev->dev;
  637. struct device_node *np = ofdev->node;
  638. struct mpc8xxx_spi_probe_info *pinfo;
  639. struct fsl_spi_platform_data *pdata;
  640. struct spi_master *master;
  641. struct resource mem;
  642. struct resource irq;
  643. const void *prop;
  644. int ret = -ENOMEM;
  645. pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
  646. if (!pinfo)
  647. return -ENOMEM;
  648. pdata = &pinfo->pdata;
  649. dev->platform_data = pdata;
  650. /* Allocate bus num dynamically. */
  651. pdata->bus_num = -1;
  652. /* SPI controller is either clocked from QE or SoC clock. */
  653. pdata->sysclk = get_brgfreq();
  654. if (pdata->sysclk == -1) {
  655. pdata->sysclk = fsl_get_sys_freq();
  656. if (pdata->sysclk == -1) {
  657. ret = -ENODEV;
  658. goto err_clk;
  659. }
  660. }
  661. prop = of_get_property(np, "mode", NULL);
  662. if (prop && !strcmp(prop, "cpu-qe"))
  663. pdata->flags = SPI_QE_CPU_MODE;
  664. ret = of_mpc8xxx_spi_get_chipselects(dev);
  665. if (ret)
  666. goto err;
  667. ret = of_address_to_resource(np, 0, &mem);
  668. if (ret)
  669. goto err;
  670. ret = of_irq_to_resource(np, 0, &irq);
  671. if (!ret) {
  672. ret = -EINVAL;
  673. goto err;
  674. }
  675. master = mpc8xxx_spi_probe(dev, &mem, irq.start);
  676. if (IS_ERR(master)) {
  677. ret = PTR_ERR(master);
  678. goto err;
  679. }
  680. of_register_spi_devices(master, np);
  681. return 0;
  682. err:
  683. of_mpc8xxx_spi_free_chipselects(dev);
  684. err_clk:
  685. kfree(pinfo);
  686. return ret;
  687. }
  688. static int __devexit of_mpc8xxx_spi_remove(struct of_device *ofdev)
  689. {
  690. int ret;
  691. ret = mpc8xxx_spi_remove(&ofdev->dev);
  692. if (ret)
  693. return ret;
  694. of_mpc8xxx_spi_free_chipselects(&ofdev->dev);
  695. return 0;
  696. }
  697. static const struct of_device_id of_mpc8xxx_spi_match[] = {
  698. { .compatible = "fsl,spi" },
  699. {},
  700. };
  701. MODULE_DEVICE_TABLE(of, of_mpc8xxx_spi_match);
  702. static struct of_platform_driver of_mpc8xxx_spi_driver = {
  703. .name = "mpc8xxx_spi",
  704. .match_table = of_mpc8xxx_spi_match,
  705. .probe = of_mpc8xxx_spi_probe,
  706. .remove = __devexit_p(of_mpc8xxx_spi_remove),
  707. };
  708. #ifdef CONFIG_MPC832x_RDB
  709. /*
  710. * XXX XXX XXX
  711. * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
  712. * only. The driver should go away soon, since newer MPC8323E-RDB's device
  713. * tree can work with OpenFirmware driver. But for now we support old trees
  714. * as well.
  715. */
  716. static int __devinit plat_mpc8xxx_spi_probe(struct platform_device *pdev)
  717. {
  718. struct resource *mem;
  719. unsigned int irq;
  720. struct spi_master *master;
  721. if (!pdev->dev.platform_data)
  722. return -EINVAL;
  723. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  724. if (!mem)
  725. return -EINVAL;
  726. irq = platform_get_irq(pdev, 0);
  727. if (!irq)
  728. return -EINVAL;
  729. master = mpc8xxx_spi_probe(&pdev->dev, mem, irq);
  730. if (IS_ERR(master))
  731. return PTR_ERR(master);
  732. return 0;
  733. }
  734. static int __devexit plat_mpc8xxx_spi_remove(struct platform_device *pdev)
  735. {
  736. return mpc8xxx_spi_remove(&pdev->dev);
  737. }
  738. MODULE_ALIAS("platform:mpc8xxx_spi");
  739. static struct platform_driver mpc8xxx_spi_driver = {
  740. .probe = plat_mpc8xxx_spi_probe,
  741. .remove = __exit_p(plat_mpc8xxx_spi_remove),
  742. .driver = {
  743. .name = "mpc8xxx_spi",
  744. .owner = THIS_MODULE,
  745. },
  746. };
  747. static bool legacy_driver_failed;
  748. static void __init legacy_driver_register(void)
  749. {
  750. legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
  751. }
  752. static void __exit legacy_driver_unregister(void)
  753. {
  754. if (legacy_driver_failed)
  755. return;
  756. platform_driver_unregister(&mpc8xxx_spi_driver);
  757. }
  758. #else
  759. static void __init legacy_driver_register(void) {}
  760. static void __exit legacy_driver_unregister(void) {}
  761. #endif /* CONFIG_MPC832x_RDB */
  762. static int __init mpc8xxx_spi_init(void)
  763. {
  764. legacy_driver_register();
  765. return of_register_platform_driver(&of_mpc8xxx_spi_driver);
  766. }
  767. static void __exit mpc8xxx_spi_exit(void)
  768. {
  769. of_unregister_platform_driver(&of_mpc8xxx_spi_driver);
  770. legacy_driver_unregister();
  771. }
  772. module_init(mpc8xxx_spi_init);
  773. module_exit(mpc8xxx_spi_exit);
  774. MODULE_AUTHOR("Kumar Gala");
  775. MODULE_DESCRIPTION("Simple MPC8xxx SPI Driver");
  776. MODULE_LICENSE("GPL");