spi_mpc83xx.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. /*
  2. * MPC83xx 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 mpc83xx_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 mpc83xx_spi {
  73. struct mpc83xx_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 mpc83xx_spi *);
  79. u32(*get_tx) (struct mpc83xx_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. bool qe_mode;
  87. u8 busy;
  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_mpc83xx_cs {
  95. /* functions to deal with different sized buffers */
  96. void (*get_rx) (u32 rx_data, struct mpc83xx_spi *);
  97. u32 (*get_tx) (struct mpc83xx_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 mpc83xx_spi_write_reg(__be32 __iomem *reg, u32 val)
  103. {
  104. out_be32(reg, val);
  105. }
  106. static inline u32 mpc83xx_spi_read_reg(__be32 __iomem *reg)
  107. {
  108. return in_be32(reg);
  109. }
  110. #define MPC83XX_SPI_RX_BUF(type) \
  111. static \
  112. void mpc83xx_spi_rx_buf_##type(u32 data, struct mpc83xx_spi *mpc83xx_spi) \
  113. { \
  114. type *rx = mpc83xx_spi->rx; \
  115. *rx++ = (type)(data >> mpc83xx_spi->rx_shift); \
  116. mpc83xx_spi->rx = rx; \
  117. }
  118. #define MPC83XX_SPI_TX_BUF(type) \
  119. static \
  120. u32 mpc83xx_spi_tx_buf_##type(struct mpc83xx_spi *mpc83xx_spi) \
  121. { \
  122. u32 data; \
  123. const type *tx = mpc83xx_spi->tx; \
  124. if (!tx) \
  125. return 0; \
  126. data = *tx++ << mpc83xx_spi->tx_shift; \
  127. mpc83xx_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 mpc83xx_spi_chipselect(struct spi_device *spi, int value)
  137. {
  138. struct mpc83xx_spi *mpc83xx_spi = spi_master_get_devdata(spi->master);
  139. struct fsl_spi_platform_data *pdata = spi->dev.parent->platform_data;
  140. bool pol = spi->mode & SPI_CS_HIGH;
  141. struct spi_mpc83xx_cs *cs = spi->controller_state;
  142. if (value == BITBANG_CS_INACTIVE) {
  143. if (pdata->cs_control)
  144. pdata->cs_control(spi, !pol);
  145. }
  146. if (value == BITBANG_CS_ACTIVE) {
  147. u32 regval = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode);
  148. mpc83xx_spi->rx_shift = cs->rx_shift;
  149. mpc83xx_spi->tx_shift = cs->tx_shift;
  150. mpc83xx_spi->get_rx = cs->get_rx;
  151. mpc83xx_spi->get_tx = cs->get_tx;
  152. if (cs->hw_mode != regval) {
  153. unsigned long flags;
  154. __be32 __iomem *mode = &mpc83xx_spi->base->mode;
  155. regval = cs->hw_mode;
  156. /* Turn off IRQs locally to minimize time that
  157. * SPI is disabled
  158. */
  159. local_irq_save(flags);
  160. /* Turn off SPI unit prior changing mode */
  161. mpc83xx_spi_write_reg(mode, regval & ~SPMODE_ENABLE);
  162. mpc83xx_spi_write_reg(mode, regval);
  163. local_irq_restore(flags);
  164. }
  165. if (pdata->cs_control)
  166. pdata->cs_control(spi, pol);
  167. }
  168. }
  169. static
  170. int mpc83xx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  171. {
  172. struct mpc83xx_spi *mpc83xx_spi;
  173. u32 regval;
  174. u8 bits_per_word, pm;
  175. u32 hz;
  176. struct spi_mpc83xx_cs *cs = spi->controller_state;
  177. mpc83xx_spi = spi_master_get_devdata(spi->master);
  178. if (t) {
  179. bits_per_word = t->bits_per_word;
  180. hz = t->speed_hz;
  181. } else {
  182. bits_per_word = 0;
  183. hz = 0;
  184. }
  185. /* spi_transfer level calls that work per-word */
  186. if (!bits_per_word)
  187. bits_per_word = spi->bits_per_word;
  188. /* Make sure its a bit width we support [4..16, 32] */
  189. if ((bits_per_word < 4)
  190. || ((bits_per_word > 16) && (bits_per_word != 32)))
  191. return -EINVAL;
  192. if (!hz)
  193. hz = spi->max_speed_hz;
  194. cs->rx_shift = 0;
  195. cs->tx_shift = 0;
  196. if (bits_per_word <= 8) {
  197. cs->get_rx = mpc83xx_spi_rx_buf_u8;
  198. cs->get_tx = mpc83xx_spi_tx_buf_u8;
  199. if (mpc83xx_spi->qe_mode) {
  200. cs->rx_shift = 16;
  201. cs->tx_shift = 24;
  202. }
  203. } else if (bits_per_word <= 16) {
  204. cs->get_rx = mpc83xx_spi_rx_buf_u16;
  205. cs->get_tx = mpc83xx_spi_tx_buf_u16;
  206. if (mpc83xx_spi->qe_mode) {
  207. cs->rx_shift = 16;
  208. cs->tx_shift = 16;
  209. }
  210. } else if (bits_per_word <= 32) {
  211. cs->get_rx = mpc83xx_spi_rx_buf_u32;
  212. cs->get_tx = mpc83xx_spi_tx_buf_u32;
  213. } else
  214. return -EINVAL;
  215. if (mpc83xx_spi->qe_mode && spi->mode & SPI_LSB_FIRST) {
  216. cs->tx_shift = 0;
  217. if (bits_per_word <= 8)
  218. cs->rx_shift = 8;
  219. else
  220. cs->rx_shift = 0;
  221. }
  222. mpc83xx_spi->rx_shift = cs->rx_shift;
  223. mpc83xx_spi->tx_shift = cs->tx_shift;
  224. mpc83xx_spi->get_rx = cs->get_rx;
  225. mpc83xx_spi->get_tx = cs->get_tx;
  226. if (bits_per_word == 32)
  227. bits_per_word = 0;
  228. else
  229. bits_per_word = bits_per_word - 1;
  230. /* mask out bits we are going to set */
  231. cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
  232. | SPMODE_PM(0xF));
  233. cs->hw_mode |= SPMODE_LEN(bits_per_word);
  234. if ((mpc83xx_spi->spibrg / hz) > 64) {
  235. cs->hw_mode |= SPMODE_DIV16;
  236. pm = mpc83xx_spi->spibrg / (hz * 64);
  237. WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. "
  238. "Will use %d Hz instead.\n", dev_name(&spi->dev),
  239. hz, mpc83xx_spi->spibrg / 1024);
  240. if (pm > 16)
  241. pm = 16;
  242. } else
  243. pm = mpc83xx_spi->spibrg / (hz * 4);
  244. if (pm)
  245. pm--;
  246. cs->hw_mode |= SPMODE_PM(pm);
  247. regval = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode);
  248. if (cs->hw_mode != regval) {
  249. unsigned long flags;
  250. __be32 __iomem *mode = &mpc83xx_spi->base->mode;
  251. regval = cs->hw_mode;
  252. /* Turn off IRQs locally to minimize time
  253. * that SPI is disabled
  254. */
  255. local_irq_save(flags);
  256. /* Turn off SPI unit prior changing mode */
  257. mpc83xx_spi_write_reg(mode, regval & ~SPMODE_ENABLE);
  258. mpc83xx_spi_write_reg(mode, regval);
  259. local_irq_restore(flags);
  260. }
  261. return 0;
  262. }
  263. static int mpc83xx_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
  264. {
  265. struct mpc83xx_spi *mpc83xx_spi;
  266. u32 word, len, bits_per_word;
  267. mpc83xx_spi = spi_master_get_devdata(spi->master);
  268. mpc83xx_spi->tx = t->tx_buf;
  269. mpc83xx_spi->rx = t->rx_buf;
  270. bits_per_word = spi->bits_per_word;
  271. if (t->bits_per_word)
  272. bits_per_word = t->bits_per_word;
  273. len = t->len;
  274. if (bits_per_word > 8) {
  275. /* invalid length? */
  276. if (len & 1)
  277. return -EINVAL;
  278. len /= 2;
  279. }
  280. if (bits_per_word > 16) {
  281. /* invalid length? */
  282. if (len & 1)
  283. return -EINVAL;
  284. len /= 2;
  285. }
  286. mpc83xx_spi->count = len;
  287. INIT_COMPLETION(mpc83xx_spi->done);
  288. /* enable rx ints */
  289. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, SPIM_NE);
  290. /* transmit word */
  291. word = mpc83xx_spi->get_tx(mpc83xx_spi);
  292. mpc83xx_spi_write_reg(&mpc83xx_spi->base->transmit, word);
  293. wait_for_completion(&mpc83xx_spi->done);
  294. /* disable rx ints */
  295. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, 0);
  296. return mpc83xx_spi->count;
  297. }
  298. static void mpc83xx_spi_work(struct work_struct *work)
  299. {
  300. struct mpc83xx_spi *mpc83xx_spi =
  301. container_of(work, struct mpc83xx_spi, work);
  302. spin_lock_irq(&mpc83xx_spi->lock);
  303. mpc83xx_spi->busy = 1;
  304. while (!list_empty(&mpc83xx_spi->queue)) {
  305. struct spi_message *m;
  306. struct spi_device *spi;
  307. struct spi_transfer *t = NULL;
  308. unsigned cs_change;
  309. int status, nsecs = 50;
  310. m = container_of(mpc83xx_spi->queue.next,
  311. struct spi_message, queue);
  312. list_del_init(&m->queue);
  313. spin_unlock_irq(&mpc83xx_spi->lock);
  314. spi = m->spi;
  315. cs_change = 1;
  316. status = 0;
  317. list_for_each_entry(t, &m->transfers, transfer_list) {
  318. if (t->bits_per_word || t->speed_hz) {
  319. /* Don't allow changes if CS is active */
  320. status = -EINVAL;
  321. if (cs_change)
  322. status = mpc83xx_spi_setup_transfer(spi, t);
  323. if (status < 0)
  324. break;
  325. }
  326. if (cs_change) {
  327. mpc83xx_spi_chipselect(spi, BITBANG_CS_ACTIVE);
  328. ndelay(nsecs);
  329. }
  330. cs_change = t->cs_change;
  331. if (t->len)
  332. status = mpc83xx_spi_bufs(spi, t);
  333. if (status) {
  334. status = -EMSGSIZE;
  335. break;
  336. }
  337. m->actual_length += t->len;
  338. if (t->delay_usecs)
  339. udelay(t->delay_usecs);
  340. if (cs_change) {
  341. ndelay(nsecs);
  342. mpc83xx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  343. ndelay(nsecs);
  344. }
  345. }
  346. m->status = status;
  347. m->complete(m->context);
  348. if (status || !cs_change) {
  349. ndelay(nsecs);
  350. mpc83xx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  351. }
  352. mpc83xx_spi_setup_transfer(spi, NULL);
  353. spin_lock_irq(&mpc83xx_spi->lock);
  354. }
  355. mpc83xx_spi->busy = 0;
  356. spin_unlock_irq(&mpc83xx_spi->lock);
  357. }
  358. static int mpc83xx_spi_setup(struct spi_device *spi)
  359. {
  360. struct mpc83xx_spi *mpc83xx_spi;
  361. int retval;
  362. u32 hw_mode;
  363. struct spi_mpc83xx_cs *cs = spi->controller_state;
  364. if (!spi->max_speed_hz)
  365. return -EINVAL;
  366. if (!cs) {
  367. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  368. if (!cs)
  369. return -ENOMEM;
  370. spi->controller_state = cs;
  371. }
  372. mpc83xx_spi = spi_master_get_devdata(spi->master);
  373. hw_mode = cs->hw_mode; /* Save orginal settings */
  374. cs->hw_mode = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode);
  375. /* mask out bits we are going to set */
  376. cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
  377. | SPMODE_REV | SPMODE_LOOP);
  378. if (spi->mode & SPI_CPHA)
  379. cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
  380. if (spi->mode & SPI_CPOL)
  381. cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
  382. if (!(spi->mode & SPI_LSB_FIRST))
  383. cs->hw_mode |= SPMODE_REV;
  384. if (spi->mode & SPI_LOOP)
  385. cs->hw_mode |= SPMODE_LOOP;
  386. retval = mpc83xx_spi_setup_transfer(spi, NULL);
  387. if (retval < 0) {
  388. cs->hw_mode = hw_mode; /* Restore settings */
  389. return retval;
  390. }
  391. #if 0 /* Don't think this is needed */
  392. /* NOTE we _need_ to call chipselect() early, ideally with adapter
  393. * setup, unless the hardware defaults cooperate to avoid confusion
  394. * between normal (active low) and inverted chipselects.
  395. */
  396. /* deselect chip (low or high) */
  397. spin_lock(&mpc83xx_spi->lock);
  398. if (!mpc83xx_spi->busy)
  399. mpc83xx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  400. spin_unlock(&mpc83xx_spi->lock);
  401. #endif
  402. return 0;
  403. }
  404. static irqreturn_t mpc83xx_spi_irq(s32 irq, void *context_data)
  405. {
  406. struct mpc83xx_spi *mpc83xx_spi = context_data;
  407. u32 event;
  408. irqreturn_t ret = IRQ_NONE;
  409. /* Get interrupt events(tx/rx) */
  410. event = mpc83xx_spi_read_reg(&mpc83xx_spi->base->event);
  411. /* We need handle RX first */
  412. if (event & SPIE_NE) {
  413. u32 rx_data = mpc83xx_spi_read_reg(&mpc83xx_spi->base->receive);
  414. if (mpc83xx_spi->rx)
  415. mpc83xx_spi->get_rx(rx_data, mpc83xx_spi);
  416. ret = IRQ_HANDLED;
  417. }
  418. if ((event & SPIE_NF) == 0)
  419. /* spin until TX is done */
  420. while (((event =
  421. mpc83xx_spi_read_reg(&mpc83xx_spi->base->event)) &
  422. SPIE_NF) == 0)
  423. cpu_relax();
  424. mpc83xx_spi->count -= 1;
  425. if (mpc83xx_spi->count) {
  426. u32 word = mpc83xx_spi->get_tx(mpc83xx_spi);
  427. mpc83xx_spi_write_reg(&mpc83xx_spi->base->transmit, word);
  428. } else {
  429. complete(&mpc83xx_spi->done);
  430. }
  431. /* Clear the events */
  432. mpc83xx_spi_write_reg(&mpc83xx_spi->base->event, event);
  433. return ret;
  434. }
  435. static int mpc83xx_spi_transfer(struct spi_device *spi,
  436. struct spi_message *m)
  437. {
  438. struct mpc83xx_spi *mpc83xx_spi = spi_master_get_devdata(spi->master);
  439. unsigned long flags;
  440. m->actual_length = 0;
  441. m->status = -EINPROGRESS;
  442. spin_lock_irqsave(&mpc83xx_spi->lock, flags);
  443. list_add_tail(&m->queue, &mpc83xx_spi->queue);
  444. queue_work(mpc83xx_spi->workqueue, &mpc83xx_spi->work);
  445. spin_unlock_irqrestore(&mpc83xx_spi->lock, flags);
  446. return 0;
  447. }
  448. static void mpc83xx_spi_cleanup(struct spi_device *spi)
  449. {
  450. kfree(spi->controller_state);
  451. }
  452. static struct spi_master * __devinit
  453. mpc83xx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq)
  454. {
  455. struct fsl_spi_platform_data *pdata = dev->platform_data;
  456. struct spi_master *master;
  457. struct mpc83xx_spi *mpc83xx_spi;
  458. u32 regval;
  459. int ret = 0;
  460. master = spi_alloc_master(dev, sizeof(struct mpc83xx_spi));
  461. if (master == NULL) {
  462. ret = -ENOMEM;
  463. goto err;
  464. }
  465. dev_set_drvdata(dev, master);
  466. /* the spi->mode bits understood by this driver: */
  467. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH
  468. | SPI_LSB_FIRST | SPI_LOOP;
  469. master->setup = mpc83xx_spi_setup;
  470. master->transfer = mpc83xx_spi_transfer;
  471. master->cleanup = mpc83xx_spi_cleanup;
  472. mpc83xx_spi = spi_master_get_devdata(master);
  473. mpc83xx_spi->qe_mode = pdata->qe_mode;
  474. mpc83xx_spi->get_rx = mpc83xx_spi_rx_buf_u8;
  475. mpc83xx_spi->get_tx = mpc83xx_spi_tx_buf_u8;
  476. mpc83xx_spi->spibrg = pdata->sysclk;
  477. mpc83xx_spi->rx_shift = 0;
  478. mpc83xx_spi->tx_shift = 0;
  479. if (mpc83xx_spi->qe_mode) {
  480. mpc83xx_spi->rx_shift = 16;
  481. mpc83xx_spi->tx_shift = 24;
  482. }
  483. init_completion(&mpc83xx_spi->done);
  484. mpc83xx_spi->base = ioremap(mem->start, mem->end - mem->start + 1);
  485. if (mpc83xx_spi->base == NULL) {
  486. ret = -ENOMEM;
  487. goto put_master;
  488. }
  489. mpc83xx_spi->irq = irq;
  490. /* Register for SPI Interrupt */
  491. ret = request_irq(mpc83xx_spi->irq, mpc83xx_spi_irq,
  492. 0, "mpc83xx_spi", mpc83xx_spi);
  493. if (ret != 0)
  494. goto unmap_io;
  495. master->bus_num = pdata->bus_num;
  496. master->num_chipselect = pdata->max_chipselect;
  497. /* SPI controller initializations */
  498. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, 0);
  499. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, 0);
  500. mpc83xx_spi_write_reg(&mpc83xx_spi->base->command, 0);
  501. mpc83xx_spi_write_reg(&mpc83xx_spi->base->event, 0xffffffff);
  502. /* Enable SPI interface */
  503. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  504. if (pdata->qe_mode)
  505. regval |= SPMODE_OP;
  506. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, regval);
  507. spin_lock_init(&mpc83xx_spi->lock);
  508. init_completion(&mpc83xx_spi->done);
  509. INIT_WORK(&mpc83xx_spi->work, mpc83xx_spi_work);
  510. INIT_LIST_HEAD(&mpc83xx_spi->queue);
  511. mpc83xx_spi->workqueue = create_singlethread_workqueue(
  512. dev_name(master->dev.parent));
  513. if (mpc83xx_spi->workqueue == NULL) {
  514. ret = -EBUSY;
  515. goto free_irq;
  516. }
  517. ret = spi_register_master(master);
  518. if (ret < 0)
  519. goto unreg_master;
  520. printk(KERN_INFO
  521. "%s: MPC83xx SPI Controller driver at 0x%p (irq = %d)\n",
  522. dev_name(dev), mpc83xx_spi->base, mpc83xx_spi->irq);
  523. return master;
  524. unreg_master:
  525. destroy_workqueue(mpc83xx_spi->workqueue);
  526. free_irq:
  527. free_irq(mpc83xx_spi->irq, mpc83xx_spi);
  528. unmap_io:
  529. iounmap(mpc83xx_spi->base);
  530. put_master:
  531. spi_master_put(master);
  532. err:
  533. return ERR_PTR(ret);
  534. }
  535. static int __devexit mpc83xx_spi_remove(struct device *dev)
  536. {
  537. struct mpc83xx_spi *mpc83xx_spi;
  538. struct spi_master *master;
  539. master = dev_get_drvdata(dev);
  540. mpc83xx_spi = spi_master_get_devdata(master);
  541. flush_workqueue(mpc83xx_spi->workqueue);
  542. destroy_workqueue(mpc83xx_spi->workqueue);
  543. spi_unregister_master(master);
  544. free_irq(mpc83xx_spi->irq, mpc83xx_spi);
  545. iounmap(mpc83xx_spi->base);
  546. return 0;
  547. }
  548. struct mpc83xx_spi_probe_info {
  549. struct fsl_spi_platform_data pdata;
  550. int *gpios;
  551. bool *alow_flags;
  552. };
  553. static struct mpc83xx_spi_probe_info *
  554. to_of_pinfo(struct fsl_spi_platform_data *pdata)
  555. {
  556. return container_of(pdata, struct mpc83xx_spi_probe_info, pdata);
  557. }
  558. static void mpc83xx_spi_cs_control(struct spi_device *spi, bool on)
  559. {
  560. struct device *dev = spi->dev.parent;
  561. struct mpc83xx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
  562. u16 cs = spi->chip_select;
  563. int gpio = pinfo->gpios[cs];
  564. bool alow = pinfo->alow_flags[cs];
  565. gpio_set_value(gpio, on ^ alow);
  566. }
  567. static int of_mpc83xx_spi_get_chipselects(struct device *dev)
  568. {
  569. struct device_node *np = dev_archdata_get_node(&dev->archdata);
  570. struct fsl_spi_platform_data *pdata = dev->platform_data;
  571. struct mpc83xx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  572. unsigned int ngpios;
  573. int i = 0;
  574. int ret;
  575. ngpios = of_gpio_count(np);
  576. if (!ngpios) {
  577. /*
  578. * SPI w/o chip-select line. One SPI device is still permitted
  579. * though.
  580. */
  581. pdata->max_chipselect = 1;
  582. return 0;
  583. }
  584. pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
  585. if (!pinfo->gpios)
  586. return -ENOMEM;
  587. memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
  588. pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
  589. GFP_KERNEL);
  590. if (!pinfo->alow_flags) {
  591. ret = -ENOMEM;
  592. goto err_alloc_flags;
  593. }
  594. for (; i < ngpios; i++) {
  595. int gpio;
  596. enum of_gpio_flags flags;
  597. gpio = of_get_gpio_flags(np, i, &flags);
  598. if (!gpio_is_valid(gpio)) {
  599. dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
  600. goto err_loop;
  601. }
  602. ret = gpio_request(gpio, dev_name(dev));
  603. if (ret) {
  604. dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
  605. goto err_loop;
  606. }
  607. pinfo->gpios[i] = gpio;
  608. pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
  609. ret = gpio_direction_output(pinfo->gpios[i],
  610. pinfo->alow_flags[i]);
  611. if (ret) {
  612. dev_err(dev, "can't set output direction for gpio "
  613. "#%d: %d\n", i, ret);
  614. goto err_loop;
  615. }
  616. }
  617. pdata->max_chipselect = ngpios;
  618. pdata->cs_control = mpc83xx_spi_cs_control;
  619. return 0;
  620. err_loop:
  621. while (i >= 0) {
  622. if (gpio_is_valid(pinfo->gpios[i]))
  623. gpio_free(pinfo->gpios[i]);
  624. i--;
  625. }
  626. kfree(pinfo->alow_flags);
  627. pinfo->alow_flags = NULL;
  628. err_alloc_flags:
  629. kfree(pinfo->gpios);
  630. pinfo->gpios = NULL;
  631. return ret;
  632. }
  633. static int of_mpc83xx_spi_free_chipselects(struct device *dev)
  634. {
  635. struct fsl_spi_platform_data *pdata = dev->platform_data;
  636. struct mpc83xx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  637. int i;
  638. if (!pinfo->gpios)
  639. return 0;
  640. for (i = 0; i < pdata->max_chipselect; i++) {
  641. if (gpio_is_valid(pinfo->gpios[i]))
  642. gpio_free(pinfo->gpios[i]);
  643. }
  644. kfree(pinfo->gpios);
  645. kfree(pinfo->alow_flags);
  646. return 0;
  647. }
  648. static int __devinit of_mpc83xx_spi_probe(struct of_device *ofdev,
  649. const struct of_device_id *ofid)
  650. {
  651. struct device *dev = &ofdev->dev;
  652. struct device_node *np = ofdev->node;
  653. struct mpc83xx_spi_probe_info *pinfo;
  654. struct fsl_spi_platform_data *pdata;
  655. struct spi_master *master;
  656. struct resource mem;
  657. struct resource irq;
  658. const void *prop;
  659. int ret = -ENOMEM;
  660. pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
  661. if (!pinfo)
  662. return -ENOMEM;
  663. pdata = &pinfo->pdata;
  664. dev->platform_data = pdata;
  665. /* Allocate bus num dynamically. */
  666. pdata->bus_num = -1;
  667. /* SPI controller is either clocked from QE or SoC clock. */
  668. pdata->sysclk = get_brgfreq();
  669. if (pdata->sysclk == -1) {
  670. pdata->sysclk = fsl_get_sys_freq();
  671. if (pdata->sysclk == -1) {
  672. ret = -ENODEV;
  673. goto err_clk;
  674. }
  675. }
  676. prop = of_get_property(np, "mode", NULL);
  677. if (prop && !strcmp(prop, "cpu-qe"))
  678. pdata->qe_mode = 1;
  679. ret = of_mpc83xx_spi_get_chipselects(dev);
  680. if (ret)
  681. goto err;
  682. ret = of_address_to_resource(np, 0, &mem);
  683. if (ret)
  684. goto err;
  685. ret = of_irq_to_resource(np, 0, &irq);
  686. if (!ret) {
  687. ret = -EINVAL;
  688. goto err;
  689. }
  690. master = mpc83xx_spi_probe(dev, &mem, irq.start);
  691. if (IS_ERR(master)) {
  692. ret = PTR_ERR(master);
  693. goto err;
  694. }
  695. of_register_spi_devices(master, np);
  696. return 0;
  697. err:
  698. of_mpc83xx_spi_free_chipselects(dev);
  699. err_clk:
  700. kfree(pinfo);
  701. return ret;
  702. }
  703. static int __devexit of_mpc83xx_spi_remove(struct of_device *ofdev)
  704. {
  705. int ret;
  706. ret = mpc83xx_spi_remove(&ofdev->dev);
  707. if (ret)
  708. return ret;
  709. of_mpc83xx_spi_free_chipselects(&ofdev->dev);
  710. return 0;
  711. }
  712. static const struct of_device_id of_mpc83xx_spi_match[] = {
  713. { .compatible = "fsl,spi" },
  714. {},
  715. };
  716. MODULE_DEVICE_TABLE(of, of_mpc83xx_spi_match);
  717. static struct of_platform_driver of_mpc83xx_spi_driver = {
  718. .name = "mpc83xx_spi",
  719. .match_table = of_mpc83xx_spi_match,
  720. .probe = of_mpc83xx_spi_probe,
  721. .remove = __devexit_p(of_mpc83xx_spi_remove),
  722. };
  723. #ifdef CONFIG_MPC832x_RDB
  724. /*
  725. * XXX XXX XXX
  726. * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
  727. * only. The driver should go away soon, since newer MPC8323E-RDB's device
  728. * tree can work with OpenFirmware driver. But for now we support old trees
  729. * as well.
  730. */
  731. static int __devinit plat_mpc83xx_spi_probe(struct platform_device *pdev)
  732. {
  733. struct resource *mem;
  734. unsigned int irq;
  735. struct spi_master *master;
  736. if (!pdev->dev.platform_data)
  737. return -EINVAL;
  738. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  739. if (!mem)
  740. return -EINVAL;
  741. irq = platform_get_irq(pdev, 0);
  742. if (!irq)
  743. return -EINVAL;
  744. master = mpc83xx_spi_probe(&pdev->dev, mem, irq);
  745. if (IS_ERR(master))
  746. return PTR_ERR(master);
  747. return 0;
  748. }
  749. static int __devexit plat_mpc83xx_spi_remove(struct platform_device *pdev)
  750. {
  751. return mpc83xx_spi_remove(&pdev->dev);
  752. }
  753. MODULE_ALIAS("platform:mpc83xx_spi");
  754. static struct platform_driver mpc83xx_spi_driver = {
  755. .probe = plat_mpc83xx_spi_probe,
  756. .remove = __exit_p(plat_mpc83xx_spi_remove),
  757. .driver = {
  758. .name = "mpc83xx_spi",
  759. .owner = THIS_MODULE,
  760. },
  761. };
  762. static bool legacy_driver_failed;
  763. static void __init legacy_driver_register(void)
  764. {
  765. legacy_driver_failed = platform_driver_register(&mpc83xx_spi_driver);
  766. }
  767. static void __exit legacy_driver_unregister(void)
  768. {
  769. if (legacy_driver_failed)
  770. return;
  771. platform_driver_unregister(&mpc83xx_spi_driver);
  772. }
  773. #else
  774. static void __init legacy_driver_register(void) {}
  775. static void __exit legacy_driver_unregister(void) {}
  776. #endif /* CONFIG_MPC832x_RDB */
  777. static int __init mpc83xx_spi_init(void)
  778. {
  779. legacy_driver_register();
  780. return of_register_platform_driver(&of_mpc83xx_spi_driver);
  781. }
  782. static void __exit mpc83xx_spi_exit(void)
  783. {
  784. of_unregister_platform_driver(&of_mpc83xx_spi_driver);
  785. legacy_driver_unregister();
  786. }
  787. module_init(mpc83xx_spi_init);
  788. module_exit(mpc83xx_spi_exit);
  789. MODULE_AUTHOR("Kumar Gala");
  790. MODULE_DESCRIPTION("Simple MPC83xx SPI Driver");
  791. MODULE_LICENSE("GPL");