blob: 76e1192eb025635b02b1b5488df5c9fb2451214b [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Mingkai Hub36ece82010-10-12 18:18:31 +08002/*
3 * Freescale SPI/eSPI controller driver library.
4 *
5 * Maintainer: Kumar Gala
6 *
7 * Copyright (C) 2006 Polycom, Inc.
8 *
9 * CPM SPI and QE buffer descriptors mode support:
10 * Copyright (c) 2009 MontaVista Software, Inc.
11 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
12 *
13 * Copyright 2010 Freescale Semiconductor, Inc.
Mingkai Hub36ece82010-10-12 18:18:31 +080014 */
Mingkai Hub36ece82010-10-12 18:18:31 +080015#include <linux/dma-mapping.h>
Xiubo Lia3108362014-09-29 10:57:06 +080016#include <linux/fsl_devices.h>
17#include <linux/interrupt.h>
18#include <linux/kernel.h>
Mingkai Hub36ece82010-10-12 18:18:31 +080019#include <linux/mm.h>
Esben Haabendal38455d7a2015-01-06 14:07:34 +010020#include <linux/module.h>
Mingkai Hub36ece82010-10-12 18:18:31 +080021#include <linux/of_platform.h>
Grant Likelyd57a4282012-04-07 14:16:53 -060022#include <linux/spi/spi.h>
Andreas Larssone8beacb2013-02-15 16:52:21 +010023#ifdef CONFIG_FSL_SOC
Mingkai Hub36ece82010-10-12 18:18:31 +080024#include <sysdev/fsl_soc.h>
Andreas Larssone8beacb2013-02-15 16:52:21 +010025#endif
Mingkai Hub36ece82010-10-12 18:18:31 +080026
Grant Likelyca632f52011-06-06 01:16:30 -060027#include "spi-fsl-lib.h"
Mingkai Hub36ece82010-10-12 18:18:31 +080028
29#define MPC8XXX_SPI_RX_BUF(type) \
30void mpc8xxx_spi_rx_buf_##type(u32 data, struct mpc8xxx_spi *mpc8xxx_spi) \
31{ \
32 type *rx = mpc8xxx_spi->rx; \
33 *rx++ = (type)(data >> mpc8xxx_spi->rx_shift); \
34 mpc8xxx_spi->rx = rx; \
Esben Haabendal38455d7a2015-01-06 14:07:34 +010035} \
36EXPORT_SYMBOL_GPL(mpc8xxx_spi_rx_buf_##type);
Mingkai Hub36ece82010-10-12 18:18:31 +080037
38#define MPC8XXX_SPI_TX_BUF(type) \
39u32 mpc8xxx_spi_tx_buf_##type(struct mpc8xxx_spi *mpc8xxx_spi) \
40{ \
41 u32 data; \
42 const type *tx = mpc8xxx_spi->tx; \
43 if (!tx) \
44 return 0; \
45 data = *tx++ << mpc8xxx_spi->tx_shift; \
46 mpc8xxx_spi->tx = tx; \
47 return data; \
Esben Haabendal38455d7a2015-01-06 14:07:34 +010048} \
49EXPORT_SYMBOL_GPL(mpc8xxx_spi_tx_buf_##type);
Mingkai Hub36ece82010-10-12 18:18:31 +080050
51MPC8XXX_SPI_RX_BUF(u8)
52MPC8XXX_SPI_RX_BUF(u16)
53MPC8XXX_SPI_RX_BUF(u32)
54MPC8XXX_SPI_TX_BUF(u8)
55MPC8XXX_SPI_TX_BUF(u16)
56MPC8XXX_SPI_TX_BUF(u32)
57
58struct mpc8xxx_spi_probe_info *to_of_pinfo(struct fsl_spi_platform_data *pdata)
59{
60 return container_of(pdata, struct mpc8xxx_spi_probe_info, pdata);
61}
Esben Haabendal38455d7a2015-01-06 14:07:34 +010062EXPORT_SYMBOL_GPL(to_of_pinfo);
Mingkai Hub36ece82010-10-12 18:18:31 +080063
Mingkai Hub36ece82010-10-12 18:18:31 +080064const char *mpc8xxx_spi_strmode(unsigned int flags)
65{
66 if (flags & SPI_QE_CPU_MODE) {
67 return "QE CPU";
68 } else if (flags & SPI_CPM_MODE) {
69 if (flags & SPI_QE)
70 return "QE";
71 else if (flags & SPI_CPM2)
72 return "CPM2";
73 else
74 return "CPM1";
75 }
76 return "CPU";
77}
Esben Haabendal38455d7a2015-01-06 14:07:34 +010078EXPORT_SYMBOL_GPL(mpc8xxx_spi_strmode);
Mingkai Hub36ece82010-10-12 18:18:31 +080079
Heiner Kallweitc592bec2014-12-03 07:56:17 +010080void mpc8xxx_spi_probe(struct device *dev, struct resource *mem,
Mingkai Hub36ece82010-10-12 18:18:31 +080081 unsigned int irq)
82{
Jingoo Han8074cf02013-07-30 16:58:59 +090083 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
Mingkai Hub36ece82010-10-12 18:18:31 +080084 struct spi_master *master;
85 struct mpc8xxx_spi *mpc8xxx_spi;
Mingkai Hub36ece82010-10-12 18:18:31 +080086
87 master = dev_get_drvdata(dev);
88
89 /* the spi->mode bits understood by this driver: */
90 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH
91 | SPI_LSB_FIRST | SPI_LOOP;
92
Mingkai Hub36ece82010-10-12 18:18:31 +080093 master->dev.of_node = dev->of_node;
94
95 mpc8xxx_spi = spi_master_get_devdata(master);
96 mpc8xxx_spi->dev = dev;
97 mpc8xxx_spi->get_rx = mpc8xxx_spi_rx_buf_u8;
98 mpc8xxx_spi->get_tx = mpc8xxx_spi_tx_buf_u8;
99 mpc8xxx_spi->flags = pdata->flags;
100 mpc8xxx_spi->spibrg = pdata->sysclk;
101 mpc8xxx_spi->irq = irq;
102
103 mpc8xxx_spi->rx_shift = 0;
104 mpc8xxx_spi->tx_shift = 0;
105
Mingkai Hub36ece82010-10-12 18:18:31 +0800106 master->bus_num = pdata->bus_num;
107 master->num_chipselect = pdata->max_chipselect;
108
Mingkai Hub36ece82010-10-12 18:18:31 +0800109 init_completion(&mpc8xxx_spi->done);
Mingkai Hub36ece82010-10-12 18:18:31 +0800110}
Esben Haabendal38455d7a2015-01-06 14:07:34 +0100111EXPORT_SYMBOL_GPL(mpc8xxx_spi_probe);
Mingkai Hub36ece82010-10-12 18:18:31 +0800112
Grant Likelyfd4a3192012-12-07 16:57:14 +0000113int of_mpc8xxx_spi_probe(struct platform_device *ofdev)
Mingkai Hub36ece82010-10-12 18:18:31 +0800114{
115 struct device *dev = &ofdev->dev;
116 struct device_node *np = ofdev->dev.of_node;
117 struct mpc8xxx_spi_probe_info *pinfo;
118 struct fsl_spi_platform_data *pdata;
119 const void *prop;
120 int ret = -ENOMEM;
121
Axel Lin72823262014-03-20 17:20:17 +0800122 pinfo = devm_kzalloc(&ofdev->dev, sizeof(*pinfo), GFP_KERNEL);
Mingkai Hub36ece82010-10-12 18:18:31 +0800123 if (!pinfo)
Zhao Qiangef4bbde2014-06-26 11:26:43 +0800124 return ret;
Mingkai Hub36ece82010-10-12 18:18:31 +0800125
126 pdata = &pinfo->pdata;
127 dev->platform_data = pdata;
128
129 /* Allocate bus num dynamically. */
130 pdata->bus_num = -1;
131
Andreas Larssone8beacb2013-02-15 16:52:21 +0100132#ifdef CONFIG_FSL_SOC
Mingkai Hub36ece82010-10-12 18:18:31 +0800133 /* SPI controller is either clocked from QE or SoC clock. */
134 pdata->sysclk = get_brgfreq();
135 if (pdata->sysclk == -1) {
136 pdata->sysclk = fsl_get_sys_freq();
Axel Lin72823262014-03-20 17:20:17 +0800137 if (pdata->sysclk == -1)
138 return -ENODEV;
Mingkai Hub36ece82010-10-12 18:18:31 +0800139 }
Andreas Larssone8beacb2013-02-15 16:52:21 +0100140#else
141 ret = of_property_read_u32(np, "clock-frequency", &pdata->sysclk);
142 if (ret)
Axel Lin72823262014-03-20 17:20:17 +0800143 return ret;
Andreas Larssone8beacb2013-02-15 16:52:21 +0100144#endif
Mingkai Hub36ece82010-10-12 18:18:31 +0800145
146 prop = of_get_property(np, "mode", NULL);
147 if (prop && !strcmp(prop, "cpu-qe"))
148 pdata->flags = SPI_QE_CPU_MODE;
149 else if (prop && !strcmp(prop, "qe"))
150 pdata->flags = SPI_CPM_MODE | SPI_QE;
151 else if (of_device_is_compatible(np, "fsl,cpm2-spi"))
152 pdata->flags = SPI_CPM_MODE | SPI_CPM2;
153 else if (of_device_is_compatible(np, "fsl,cpm1-spi"))
154 pdata->flags = SPI_CPM_MODE | SPI_CPM1;
155
156 return 0;
Mingkai Hub36ece82010-10-12 18:18:31 +0800157}
Esben Haabendal38455d7a2015-01-06 14:07:34 +0100158EXPORT_SYMBOL_GPL(of_mpc8xxx_spi_probe);
159
160MODULE_LICENSE("GPL");