blob: 1e43412cd9f88921519f4d981c7a3229afc4a248 [file] [log] [blame]
Mingkai Hub36ece82010-10-12 18:18:31 +08001/*
2 * Freescale SPI/eSPI controller driver library.
3 *
4 * Maintainer: Kumar Gala
5 *
6 * Copyright (C) 2006 Polycom, Inc.
7 *
8 * CPM SPI and QE buffer descriptors mode support:
9 * Copyright (c) 2009 MontaVista Software, Inc.
10 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
11 *
12 * Copyright 2010 Freescale Semiconductor, Inc.
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
18 */
Mingkai Hub36ece82010-10-12 18:18:31 +080019#include <linux/dma-mapping.h>
Xiubo Lia3108362014-09-29 10:57:06 +080020#include <linux/fsl_devices.h>
21#include <linux/interrupt.h>
22#include <linux/kernel.h>
Mingkai Hub36ece82010-10-12 18:18:31 +080023#include <linux/mm.h>
Esben Haabendal38455d7a2015-01-06 14:07:34 +010024#include <linux/module.h>
Mingkai Hub36ece82010-10-12 18:18:31 +080025#include <linux/of_platform.h>
Grant Likelyd57a4282012-04-07 14:16:53 -060026#include <linux/spi/spi.h>
Andreas Larssone8beacb2013-02-15 16:52:21 +010027#ifdef CONFIG_FSL_SOC
Mingkai Hub36ece82010-10-12 18:18:31 +080028#include <sysdev/fsl_soc.h>
Andreas Larssone8beacb2013-02-15 16:52:21 +010029#endif
Mingkai Hub36ece82010-10-12 18:18:31 +080030
Grant Likelyca632f52011-06-06 01:16:30 -060031#include "spi-fsl-lib.h"
Mingkai Hub36ece82010-10-12 18:18:31 +080032
33#define MPC8XXX_SPI_RX_BUF(type) \
34void mpc8xxx_spi_rx_buf_##type(u32 data, struct mpc8xxx_spi *mpc8xxx_spi) \
35{ \
36 type *rx = mpc8xxx_spi->rx; \
37 *rx++ = (type)(data >> mpc8xxx_spi->rx_shift); \
38 mpc8xxx_spi->rx = rx; \
Esben Haabendal38455d7a2015-01-06 14:07:34 +010039} \
40EXPORT_SYMBOL_GPL(mpc8xxx_spi_rx_buf_##type);
Mingkai Hub36ece82010-10-12 18:18:31 +080041
42#define MPC8XXX_SPI_TX_BUF(type) \
43u32 mpc8xxx_spi_tx_buf_##type(struct mpc8xxx_spi *mpc8xxx_spi) \
44{ \
45 u32 data; \
46 const type *tx = mpc8xxx_spi->tx; \
47 if (!tx) \
48 return 0; \
49 data = *tx++ << mpc8xxx_spi->tx_shift; \
50 mpc8xxx_spi->tx = tx; \
51 return data; \
Esben Haabendal38455d7a2015-01-06 14:07:34 +010052} \
53EXPORT_SYMBOL_GPL(mpc8xxx_spi_tx_buf_##type);
Mingkai Hub36ece82010-10-12 18:18:31 +080054
55MPC8XXX_SPI_RX_BUF(u8)
56MPC8XXX_SPI_RX_BUF(u16)
57MPC8XXX_SPI_RX_BUF(u32)
58MPC8XXX_SPI_TX_BUF(u8)
59MPC8XXX_SPI_TX_BUF(u16)
60MPC8XXX_SPI_TX_BUF(u32)
61
62struct mpc8xxx_spi_probe_info *to_of_pinfo(struct fsl_spi_platform_data *pdata)
63{
64 return container_of(pdata, struct mpc8xxx_spi_probe_info, pdata);
65}
Esben Haabendal38455d7a2015-01-06 14:07:34 +010066EXPORT_SYMBOL_GPL(to_of_pinfo);
Mingkai Hub36ece82010-10-12 18:18:31 +080067
Mingkai Hub36ece82010-10-12 18:18:31 +080068const char *mpc8xxx_spi_strmode(unsigned int flags)
69{
70 if (flags & SPI_QE_CPU_MODE) {
71 return "QE CPU";
72 } else if (flags & SPI_CPM_MODE) {
73 if (flags & SPI_QE)
74 return "QE";
75 else if (flags & SPI_CPM2)
76 return "CPM2";
77 else
78 return "CPM1";
79 }
80 return "CPU";
81}
Esben Haabendal38455d7a2015-01-06 14:07:34 +010082EXPORT_SYMBOL_GPL(mpc8xxx_spi_strmode);
Mingkai Hub36ece82010-10-12 18:18:31 +080083
Heiner Kallweitc592bec2014-12-03 07:56:17 +010084void mpc8xxx_spi_probe(struct device *dev, struct resource *mem,
Mingkai Hub36ece82010-10-12 18:18:31 +080085 unsigned int irq)
86{
Jingoo Han8074cf02013-07-30 16:58:59 +090087 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
Mingkai Hub36ece82010-10-12 18:18:31 +080088 struct spi_master *master;
89 struct mpc8xxx_spi *mpc8xxx_spi;
Mingkai Hub36ece82010-10-12 18:18:31 +080090
91 master = dev_get_drvdata(dev);
92
93 /* the spi->mode bits understood by this driver: */
94 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH
95 | SPI_LSB_FIRST | SPI_LOOP;
96
Mingkai Hub36ece82010-10-12 18:18:31 +080097 master->dev.of_node = dev->of_node;
98
99 mpc8xxx_spi = spi_master_get_devdata(master);
100 mpc8xxx_spi->dev = dev;
101 mpc8xxx_spi->get_rx = mpc8xxx_spi_rx_buf_u8;
102 mpc8xxx_spi->get_tx = mpc8xxx_spi_tx_buf_u8;
103 mpc8xxx_spi->flags = pdata->flags;
104 mpc8xxx_spi->spibrg = pdata->sysclk;
105 mpc8xxx_spi->irq = irq;
106
107 mpc8xxx_spi->rx_shift = 0;
108 mpc8xxx_spi->tx_shift = 0;
109
Mingkai Hub36ece82010-10-12 18:18:31 +0800110 master->bus_num = pdata->bus_num;
111 master->num_chipselect = pdata->max_chipselect;
112
Mingkai Hub36ece82010-10-12 18:18:31 +0800113 init_completion(&mpc8xxx_spi->done);
Mingkai Hub36ece82010-10-12 18:18:31 +0800114}
Esben Haabendal38455d7a2015-01-06 14:07:34 +0100115EXPORT_SYMBOL_GPL(mpc8xxx_spi_probe);
Mingkai Hub36ece82010-10-12 18:18:31 +0800116
Grant Likelyfd4a3192012-12-07 16:57:14 +0000117int of_mpc8xxx_spi_probe(struct platform_device *ofdev)
Mingkai Hub36ece82010-10-12 18:18:31 +0800118{
119 struct device *dev = &ofdev->dev;
120 struct device_node *np = ofdev->dev.of_node;
121 struct mpc8xxx_spi_probe_info *pinfo;
122 struct fsl_spi_platform_data *pdata;
123 const void *prop;
124 int ret = -ENOMEM;
125
Axel Lin72823262014-03-20 17:20:17 +0800126 pinfo = devm_kzalloc(&ofdev->dev, sizeof(*pinfo), GFP_KERNEL);
Mingkai Hub36ece82010-10-12 18:18:31 +0800127 if (!pinfo)
Zhao Qiangef4bbde2014-06-26 11:26:43 +0800128 return ret;
Mingkai Hub36ece82010-10-12 18:18:31 +0800129
130 pdata = &pinfo->pdata;
131 dev->platform_data = pdata;
132
133 /* Allocate bus num dynamically. */
134 pdata->bus_num = -1;
135
Andreas Larssone8beacb2013-02-15 16:52:21 +0100136#ifdef CONFIG_FSL_SOC
Mingkai Hub36ece82010-10-12 18:18:31 +0800137 /* SPI controller is either clocked from QE or SoC clock. */
138 pdata->sysclk = get_brgfreq();
139 if (pdata->sysclk == -1) {
140 pdata->sysclk = fsl_get_sys_freq();
Axel Lin72823262014-03-20 17:20:17 +0800141 if (pdata->sysclk == -1)
142 return -ENODEV;
Mingkai Hub36ece82010-10-12 18:18:31 +0800143 }
Andreas Larssone8beacb2013-02-15 16:52:21 +0100144#else
145 ret = of_property_read_u32(np, "clock-frequency", &pdata->sysclk);
146 if (ret)
Axel Lin72823262014-03-20 17:20:17 +0800147 return ret;
Andreas Larssone8beacb2013-02-15 16:52:21 +0100148#endif
Mingkai Hub36ece82010-10-12 18:18:31 +0800149
150 prop = of_get_property(np, "mode", NULL);
151 if (prop && !strcmp(prop, "cpu-qe"))
152 pdata->flags = SPI_QE_CPU_MODE;
153 else if (prop && !strcmp(prop, "qe"))
154 pdata->flags = SPI_CPM_MODE | SPI_QE;
155 else if (of_device_is_compatible(np, "fsl,cpm2-spi"))
156 pdata->flags = SPI_CPM_MODE | SPI_CPM2;
157 else if (of_device_is_compatible(np, "fsl,cpm1-spi"))
158 pdata->flags = SPI_CPM_MODE | SPI_CPM1;
159
160 return 0;
Mingkai Hub36ece82010-10-12 18:18:31 +0800161}
Esben Haabendal38455d7a2015-01-06 14:07:34 +0100162EXPORT_SYMBOL_GPL(of_mpc8xxx_spi_probe);
163
164MODULE_LICENSE("GPL");