spi_mpc83xx.c 23 KB

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