blob: 647d231d4422ce49c436164e959029d2526a9afc [file] [log] [blame]
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001/*
2 * Support for the Tundra TSI148 VME-PCI Bridge Chip
3 *
Martyn Welch66bd8db2010-02-18 15:12:52 +00004 * Author: Martyn Welch <martyn.welch@ge.com>
5 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
Martyn Welchd22b8ed2009-07-31 09:28:17 +01006 *
7 * Based on work by Tom Armistead and Ajit Prem
8 * Copyright 2004 Motorola Inc.
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 */
15
Martyn Welchd22b8ed2009-07-31 09:28:17 +010016#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/mm.h>
19#include <linux/types.h>
20#include <linux/errno.h>
21#include <linux/proc_fs.h>
22#include <linux/pci.h>
23#include <linux/poll.h>
24#include <linux/dma-mapping.h>
25#include <linux/interrupt.h>
26#include <linux/spinlock.h>
Greg Kroah-Hartman6af783c2009-10-12 15:00:08 -070027#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Martyn Welch79463282010-03-22 14:58:57 +000029#include <linux/time.h>
30#include <linux/io.h>
31#include <linux/uaccess.h>
Martyn Welchac1a4f22012-03-22 13:27:30 +000032#include <linux/byteorder/generic.h>
Greg Kroah-Hartmandb3b9e92012-04-26 12:34:58 -070033#include <linux/vme.h>
Martyn Welchd22b8ed2009-07-31 09:28:17 +010034
Martyn Welchd22b8ed2009-07-31 09:28:17 +010035#include "../vme_bridge.h"
36#include "vme_tsi148.h"
37
Martyn Welchd22b8ed2009-07-31 09:28:17 +010038static int tsi148_probe(struct pci_dev *, const struct pci_device_id *);
39static void tsi148_remove(struct pci_dev *);
Martyn Welchd22b8ed2009-07-31 09:28:17 +010040
41
Martyn Welch29848ac2010-02-18 15:13:05 +000042/* Module parameter */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103043static bool err_chk;
Martyn Welch638f1992009-12-15 08:42:49 +000044static int geoid;
Martyn Welchd22b8ed2009-07-31 09:28:17 +010045
Vincent Bossier584721c2011-06-03 10:07:39 +010046static const char driver_name[] = "vme_tsi148";
Martyn Welchd22b8ed2009-07-31 09:28:17 +010047
Jingoo Hanc3a09c12013-12-03 08:29:48 +090048static const struct pci_device_id tsi148_ids[] = {
Martyn Welchd22b8ed2009-07-31 09:28:17 +010049 { PCI_DEVICE(PCI_VENDOR_ID_TUNDRA, PCI_DEVICE_ID_TUNDRA_TSI148) },
50 { },
51};
52
Alessio Igor Bogani553ebb82016-06-14 16:36:55 +020053MODULE_DEVICE_TABLE(pci, tsi148_ids);
54
Martyn Welchd22b8ed2009-07-31 09:28:17 +010055static struct pci_driver tsi148_driver = {
56 .name = driver_name,
57 .id_table = tsi148_ids,
58 .probe = tsi148_probe,
59 .remove = tsi148_remove,
60};
61
62static void reg_join(unsigned int high, unsigned int low,
63 unsigned long long *variable)
64{
65 *variable = (unsigned long long)high << 32;
66 *variable |= (unsigned long long)low;
67}
68
69static void reg_split(unsigned long long variable, unsigned int *high,
70 unsigned int *low)
71{
72 *low = (unsigned int)variable & 0xFFFFFFFF;
73 *high = (unsigned int)(variable >> 32);
74}
75
76/*
77 * Wakes up DMA queue.
78 */
Martyn Welch29848ac2010-02-18 15:13:05 +000079static u32 tsi148_DMA_irqhandler(struct tsi148_driver *bridge,
80 int channel_mask)
Martyn Welchd22b8ed2009-07-31 09:28:17 +010081{
82 u32 serviced = 0;
83
84 if (channel_mask & TSI148_LCSR_INTS_DMA0S) {
Emilio G. Cota886953e2010-11-12 11:14:07 +000085 wake_up(&bridge->dma_queue[0]);
Martyn Welchd22b8ed2009-07-31 09:28:17 +010086 serviced |= TSI148_LCSR_INTC_DMA0C;
87 }
88 if (channel_mask & TSI148_LCSR_INTS_DMA1S) {
Emilio G. Cota886953e2010-11-12 11:14:07 +000089 wake_up(&bridge->dma_queue[1]);
Martyn Welchd22b8ed2009-07-31 09:28:17 +010090 serviced |= TSI148_LCSR_INTC_DMA1C;
91 }
92
93 return serviced;
94}
95
96/*
97 * Wake up location monitor queue
98 */
Martyn Welch29848ac2010-02-18 15:13:05 +000099static u32 tsi148_LM_irqhandler(struct tsi148_driver *bridge, u32 stat)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100100{
101 int i;
102 u32 serviced = 0;
103
104 for (i = 0; i < 4; i++) {
Martyn Welch79463282010-03-22 14:58:57 +0000105 if (stat & TSI148_LCSR_INTS_LMS[i]) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100106 /* We only enable interrupts if the callback is set */
Aaron Sierrafa54b322016-04-29 16:41:02 -0500107 bridge->lm_callback[i](bridge->lm_data[i]);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100108 serviced |= TSI148_LCSR_INTC_LMC[i];
109 }
110 }
111
112 return serviced;
113}
114
115/*
116 * Wake up mail box queue.
117 *
118 * XXX This functionality is not exposed up though API.
119 */
Martyn Welch48d93562010-03-22 14:58:50 +0000120static u32 tsi148_MB_irqhandler(struct vme_bridge *tsi148_bridge, u32 stat)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100121{
122 int i;
123 u32 val;
124 u32 serviced = 0;
Martyn Welch48d93562010-03-22 14:58:50 +0000125 struct tsi148_driver *bridge;
126
127 bridge = tsi148_bridge->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100128
129 for (i = 0; i < 4; i++) {
Martyn Welch79463282010-03-22 14:58:57 +0000130 if (stat & TSI148_LCSR_INTS_MBS[i]) {
Martyn Welch29848ac2010-02-18 15:13:05 +0000131 val = ioread32be(bridge->base + TSI148_GCSR_MBOX[i]);
Martyn Welch48d93562010-03-22 14:58:50 +0000132 dev_err(tsi148_bridge->parent, "VME Mailbox %d received"
133 ": 0x%x\n", i, val);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100134 serviced |= TSI148_LCSR_INTC_MBC[i];
135 }
136 }
137
138 return serviced;
139}
140
141/*
142 * Display error & status message when PERR (PCI) exception interrupt occurs.
143 */
Martyn Welch48d93562010-03-22 14:58:50 +0000144static u32 tsi148_PERR_irqhandler(struct vme_bridge *tsi148_bridge)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100145{
Martyn Welch48d93562010-03-22 14:58:50 +0000146 struct tsi148_driver *bridge;
147
148 bridge = tsi148_bridge->driver_priv;
149
150 dev_err(tsi148_bridge->parent, "PCI Exception at address: 0x%08x:%08x, "
151 "attributes: %08x\n",
Martyn Welch29848ac2010-02-18 15:13:05 +0000152 ioread32be(bridge->base + TSI148_LCSR_EDPAU),
153 ioread32be(bridge->base + TSI148_LCSR_EDPAL),
Martyn Welch48d93562010-03-22 14:58:50 +0000154 ioread32be(bridge->base + TSI148_LCSR_EDPAT));
155
156 dev_err(tsi148_bridge->parent, "PCI-X attribute reg: %08x, PCI-X split "
157 "completion reg: %08x\n",
Martyn Welch29848ac2010-02-18 15:13:05 +0000158 ioread32be(bridge->base + TSI148_LCSR_EDPXA),
Martyn Welch48d93562010-03-22 14:58:50 +0000159 ioread32be(bridge->base + TSI148_LCSR_EDPXS));
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100160
Martyn Welch29848ac2010-02-18 15:13:05 +0000161 iowrite32be(TSI148_LCSR_EDPAT_EDPCL, bridge->base + TSI148_LCSR_EDPAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100162
163 return TSI148_LCSR_INTC_PERRC;
164}
165
166/*
167 * Save address and status when VME error interrupt occurs.
168 */
Martyn Welch29848ac2010-02-18 15:13:05 +0000169static u32 tsi148_VERR_irqhandler(struct vme_bridge *tsi148_bridge)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100170{
171 unsigned int error_addr_high, error_addr_low;
172 unsigned long long error_addr;
173 u32 error_attrib;
Dmitry Kalinkin472f16f2015-09-18 02:01:43 +0300174 int error_am;
Martyn Welch29848ac2010-02-18 15:13:05 +0000175 struct tsi148_driver *bridge;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100176
Martyn Welch29848ac2010-02-18 15:13:05 +0000177 bridge = tsi148_bridge->driver_priv;
178
179 error_addr_high = ioread32be(bridge->base + TSI148_LCSR_VEAU);
180 error_addr_low = ioread32be(bridge->base + TSI148_LCSR_VEAL);
181 error_attrib = ioread32be(bridge->base + TSI148_LCSR_VEAT);
Dmitry Kalinkin472f16f2015-09-18 02:01:43 +0300182 error_am = (error_attrib & TSI148_LCSR_VEAT_AM_M) >> 8;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100183
184 reg_join(error_addr_high, error_addr_low, &error_addr);
185
186 /* Check for exception register overflow (we have lost error data) */
Martyn Welch79463282010-03-22 14:58:57 +0000187 if (error_attrib & TSI148_LCSR_VEAT_VEOF) {
Martyn Welch48d93562010-03-22 14:58:50 +0000188 dev_err(tsi148_bridge->parent, "VME Bus Exception Overflow "
189 "Occurred\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100190 }
191
Dmitry Kalinkine2c63932015-09-18 02:01:42 +0300192 if (err_chk)
Dmitry Kalinkin472f16f2015-09-18 02:01:43 +0300193 vme_bus_error_handler(tsi148_bridge, error_addr, error_am);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +0300194 else
Martyn Welche31c51e2013-06-11 11:20:17 +0100195 dev_err(tsi148_bridge->parent,
196 "VME Bus Error at address: 0x%llx, attributes: %08x\n",
197 error_addr, error_attrib);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100198
199 /* Clear Status */
Martyn Welch29848ac2010-02-18 15:13:05 +0000200 iowrite32be(TSI148_LCSR_VEAT_VESCL, bridge->base + TSI148_LCSR_VEAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100201
202 return TSI148_LCSR_INTC_VERRC;
203}
204
205/*
206 * Wake up IACK queue.
207 */
Martyn Welch29848ac2010-02-18 15:13:05 +0000208static u32 tsi148_IACK_irqhandler(struct tsi148_driver *bridge)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100209{
Emilio G. Cota886953e2010-11-12 11:14:07 +0000210 wake_up(&bridge->iack_queue);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100211
212 return TSI148_LCSR_INTC_IACKC;
213}
214
215/*
216 * Calling VME bus interrupt callback if provided.
217 */
Martyn Welch29848ac2010-02-18 15:13:05 +0000218static u32 tsi148_VIRQ_irqhandler(struct vme_bridge *tsi148_bridge,
219 u32 stat)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100220{
221 int vec, i, serviced = 0;
Martyn Welch29848ac2010-02-18 15:13:05 +0000222 struct tsi148_driver *bridge;
223
224 bridge = tsi148_bridge->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100225
226 for (i = 7; i > 0; i--) {
227 if (stat & (1 << i)) {
228 /*
Martyn Welch79463282010-03-22 14:58:57 +0000229 * Note: Even though the registers are defined as
230 * 32-bits in the spec, we only want to issue 8-bit
231 * IACK cycles on the bus, read from offset 3.
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100232 */
Martyn Welch29848ac2010-02-18 15:13:05 +0000233 vec = ioread8(bridge->base + TSI148_LCSR_VIACK[i] + 3);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100234
Martyn Welchc813f592009-10-29 16:34:54 +0000235 vme_irq_handler(tsi148_bridge, i, vec);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100236
237 serviced |= (1 << i);
238 }
239 }
240
241 return serviced;
242}
243
244/*
245 * Top level interrupt handler. Clears appropriate interrupt status bits and
246 * then calls appropriate sub handler(s).
247 */
Martyn Welch29848ac2010-02-18 15:13:05 +0000248static irqreturn_t tsi148_irqhandler(int irq, void *ptr)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100249{
250 u32 stat, enable, serviced = 0;
Martyn Welch29848ac2010-02-18 15:13:05 +0000251 struct vme_bridge *tsi148_bridge;
252 struct tsi148_driver *bridge;
253
254 tsi148_bridge = ptr;
255
256 bridge = tsi148_bridge->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100257
258 /* Determine which interrupts are unmasked and set */
Martyn Welch29848ac2010-02-18 15:13:05 +0000259 enable = ioread32be(bridge->base + TSI148_LCSR_INTEO);
260 stat = ioread32be(bridge->base + TSI148_LCSR_INTS);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100261
262 /* Only look at unmasked interrupts */
263 stat &= enable;
264
Martyn Welch79463282010-03-22 14:58:57 +0000265 if (unlikely(!stat))
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100266 return IRQ_NONE;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100267
268 /* Call subhandlers as appropriate */
269 /* DMA irqs */
270 if (stat & (TSI148_LCSR_INTS_DMA1S | TSI148_LCSR_INTS_DMA0S))
Martyn Welch29848ac2010-02-18 15:13:05 +0000271 serviced |= tsi148_DMA_irqhandler(bridge, stat);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100272
273 /* Location monitor irqs */
274 if (stat & (TSI148_LCSR_INTS_LM3S | TSI148_LCSR_INTS_LM2S |
275 TSI148_LCSR_INTS_LM1S | TSI148_LCSR_INTS_LM0S))
Martyn Welch29848ac2010-02-18 15:13:05 +0000276 serviced |= tsi148_LM_irqhandler(bridge, stat);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100277
278 /* Mail box irqs */
279 if (stat & (TSI148_LCSR_INTS_MB3S | TSI148_LCSR_INTS_MB2S |
280 TSI148_LCSR_INTS_MB1S | TSI148_LCSR_INTS_MB0S))
Martyn Welch48d93562010-03-22 14:58:50 +0000281 serviced |= tsi148_MB_irqhandler(tsi148_bridge, stat);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100282
283 /* PCI bus error */
284 if (stat & TSI148_LCSR_INTS_PERRS)
Martyn Welch48d93562010-03-22 14:58:50 +0000285 serviced |= tsi148_PERR_irqhandler(tsi148_bridge);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100286
287 /* VME bus error */
288 if (stat & TSI148_LCSR_INTS_VERRS)
Martyn Welch29848ac2010-02-18 15:13:05 +0000289 serviced |= tsi148_VERR_irqhandler(tsi148_bridge);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100290
291 /* IACK irq */
292 if (stat & TSI148_LCSR_INTS_IACKS)
Martyn Welch29848ac2010-02-18 15:13:05 +0000293 serviced |= tsi148_IACK_irqhandler(bridge);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100294
295 /* VME bus irqs */
296 if (stat & (TSI148_LCSR_INTS_IRQ7S | TSI148_LCSR_INTS_IRQ6S |
297 TSI148_LCSR_INTS_IRQ5S | TSI148_LCSR_INTS_IRQ4S |
298 TSI148_LCSR_INTS_IRQ3S | TSI148_LCSR_INTS_IRQ2S |
299 TSI148_LCSR_INTS_IRQ1S))
Martyn Welch29848ac2010-02-18 15:13:05 +0000300 serviced |= tsi148_VIRQ_irqhandler(tsi148_bridge, stat);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100301
302 /* Clear serviced interrupts */
Martyn Welch29848ac2010-02-18 15:13:05 +0000303 iowrite32be(serviced, bridge->base + TSI148_LCSR_INTC);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100304
305 return IRQ_HANDLED;
306}
307
Martyn Welch29848ac2010-02-18 15:13:05 +0000308static int tsi148_irq_init(struct vme_bridge *tsi148_bridge)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100309{
310 int result;
311 unsigned int tmp;
312 struct pci_dev *pdev;
Martyn Welch29848ac2010-02-18 15:13:05 +0000313 struct tsi148_driver *bridge;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100314
Aaron Sierra177581fa2014-04-03 14:48:27 -0500315 pdev = to_pci_dev(tsi148_bridge->parent);
Martyn Welch29848ac2010-02-18 15:13:05 +0000316
317 bridge = tsi148_bridge->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100318
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100319 result = request_irq(pdev->irq,
320 tsi148_irqhandler,
321 IRQF_SHARED,
Martyn Welch29848ac2010-02-18 15:13:05 +0000322 driver_name, tsi148_bridge);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100323 if (result) {
Martyn Welch48d93562010-03-22 14:58:50 +0000324 dev_err(tsi148_bridge->parent, "Can't get assigned pci irq "
325 "vector %02X\n", pdev->irq);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100326 return result;
327 }
328
329 /* Enable and unmask interrupts */
330 tmp = TSI148_LCSR_INTEO_DMA1EO | TSI148_LCSR_INTEO_DMA0EO |
331 TSI148_LCSR_INTEO_MB3EO | TSI148_LCSR_INTEO_MB2EO |
332 TSI148_LCSR_INTEO_MB1EO | TSI148_LCSR_INTEO_MB0EO |
333 TSI148_LCSR_INTEO_PERREO | TSI148_LCSR_INTEO_VERREO |
334 TSI148_LCSR_INTEO_IACKEO;
335
Martyn Welch29848ac2010-02-18 15:13:05 +0000336 /* This leaves the following interrupts masked.
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100337 * TSI148_LCSR_INTEO_VIEEO
338 * TSI148_LCSR_INTEO_SYSFLEO
339 * TSI148_LCSR_INTEO_ACFLEO
340 */
341
342 /* Don't enable Location Monitor interrupts here - they will be
343 * enabled when the location monitors are properly configured and
344 * a callback has been attached.
345 * TSI148_LCSR_INTEO_LM0EO
346 * TSI148_LCSR_INTEO_LM1EO
347 * TSI148_LCSR_INTEO_LM2EO
348 * TSI148_LCSR_INTEO_LM3EO
349 */
350
351 /* Don't enable VME interrupts until we add a handler, else the board
352 * will respond to it and we don't want that unless it knows how to
353 * properly deal with it.
354 * TSI148_LCSR_INTEO_IRQ7EO
355 * TSI148_LCSR_INTEO_IRQ6EO
356 * TSI148_LCSR_INTEO_IRQ5EO
357 * TSI148_LCSR_INTEO_IRQ4EO
358 * TSI148_LCSR_INTEO_IRQ3EO
359 * TSI148_LCSR_INTEO_IRQ2EO
360 * TSI148_LCSR_INTEO_IRQ1EO
361 */
362
363 iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEO);
364 iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEN);
365
366 return 0;
367}
368
Emilio G. Cotaa82ad052010-11-12 11:14:47 +0000369static void tsi148_irq_exit(struct vme_bridge *tsi148_bridge,
370 struct pci_dev *pdev)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100371{
Emilio G. Cotaa82ad052010-11-12 11:14:47 +0000372 struct tsi148_driver *bridge = tsi148_bridge->driver_priv;
373
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100374 /* Turn off interrupts */
Martyn Welch29848ac2010-02-18 15:13:05 +0000375 iowrite32be(0x0, bridge->base + TSI148_LCSR_INTEO);
376 iowrite32be(0x0, bridge->base + TSI148_LCSR_INTEN);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100377
378 /* Clear all interrupts */
Martyn Welch29848ac2010-02-18 15:13:05 +0000379 iowrite32be(0xFFFFFFFF, bridge->base + TSI148_LCSR_INTC);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100380
381 /* Detach interrupt handler */
Emilio G. Cotaa82ad052010-11-12 11:14:47 +0000382 free_irq(pdev->irq, tsi148_bridge);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100383}
384
385/*
386 * Check to see if an IACk has been received, return true (1) or false (0).
387 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +0000388static int tsi148_iack_received(struct tsi148_driver *bridge)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100389{
390 u32 tmp;
391
Martyn Welch29848ac2010-02-18 15:13:05 +0000392 tmp = ioread32be(bridge->base + TSI148_LCSR_VICR);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100393
394 if (tmp & TSI148_LCSR_VICR_IRQS)
395 return 0;
396 else
397 return 1;
398}
399
400/*
Martyn Welchc813f592009-10-29 16:34:54 +0000401 * Configure VME interrupt
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100402 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +0000403static void tsi148_irq_set(struct vme_bridge *tsi148_bridge, int level,
Martyn Welch29848ac2010-02-18 15:13:05 +0000404 int state, int sync)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100405{
Martyn Welch75155022009-08-11 13:50:49 +0100406 struct pci_dev *pdev;
Martyn Welchc813f592009-10-29 16:34:54 +0000407 u32 tmp;
Martyn Welch29848ac2010-02-18 15:13:05 +0000408 struct tsi148_driver *bridge;
409
410 bridge = tsi148_bridge->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100411
Martyn Welchc813f592009-10-29 16:34:54 +0000412 /* We need to do the ordering differently for enabling and disabling */
413 if (state == 0) {
Martyn Welch29848ac2010-02-18 15:13:05 +0000414 tmp = ioread32be(bridge->base + TSI148_LCSR_INTEN);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100415 tmp &= ~TSI148_LCSR_INTEN_IRQEN[level - 1];
Martyn Welch29848ac2010-02-18 15:13:05 +0000416 iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEN);
Martyn Welchdf455172009-08-05 17:38:31 +0100417
Martyn Welch29848ac2010-02-18 15:13:05 +0000418 tmp = ioread32be(bridge->base + TSI148_LCSR_INTEO);
Martyn Welchdf455172009-08-05 17:38:31 +0100419 tmp &= ~TSI148_LCSR_INTEO_IRQEO[level - 1];
Martyn Welch29848ac2010-02-18 15:13:05 +0000420 iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEO);
Martyn Welch75155022009-08-11 13:50:49 +0100421
Martyn Welchc813f592009-10-29 16:34:54 +0000422 if (sync != 0) {
Aaron Sierra177581fa2014-04-03 14:48:27 -0500423 pdev = to_pci_dev(tsi148_bridge->parent);
Martyn Welchc813f592009-10-29 16:34:54 +0000424 synchronize_irq(pdev->irq);
425 }
426 } else {
Martyn Welch29848ac2010-02-18 15:13:05 +0000427 tmp = ioread32be(bridge->base + TSI148_LCSR_INTEO);
Martyn Welchc813f592009-10-29 16:34:54 +0000428 tmp |= TSI148_LCSR_INTEO_IRQEO[level - 1];
Martyn Welch29848ac2010-02-18 15:13:05 +0000429 iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEO);
Martyn Welchc813f592009-10-29 16:34:54 +0000430
Martyn Welch29848ac2010-02-18 15:13:05 +0000431 tmp = ioread32be(bridge->base + TSI148_LCSR_INTEN);
Martyn Welchc813f592009-10-29 16:34:54 +0000432 tmp |= TSI148_LCSR_INTEN_IRQEN[level - 1];
Martyn Welch29848ac2010-02-18 15:13:05 +0000433 iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEN);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100434 }
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100435}
436
437/*
438 * Generate a VME bus interrupt at the requested level & vector. Wait for
439 * interrupt to be acked.
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100440 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +0000441static int tsi148_irq_generate(struct vme_bridge *tsi148_bridge, int level,
442 int statid)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100443{
444 u32 tmp;
Martyn Welch29848ac2010-02-18 15:13:05 +0000445 struct tsi148_driver *bridge;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100446
Martyn Welch29848ac2010-02-18 15:13:05 +0000447 bridge = tsi148_bridge->driver_priv;
448
Emilio G. Cota886953e2010-11-12 11:14:07 +0000449 mutex_lock(&bridge->vme_int);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100450
451 /* Read VICR register */
Martyn Welch29848ac2010-02-18 15:13:05 +0000452 tmp = ioread32be(bridge->base + TSI148_LCSR_VICR);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100453
454 /* Set Status/ID */
455 tmp = (tmp & ~TSI148_LCSR_VICR_STID_M) |
456 (statid & TSI148_LCSR_VICR_STID_M);
Martyn Welch29848ac2010-02-18 15:13:05 +0000457 iowrite32be(tmp, bridge->base + TSI148_LCSR_VICR);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100458
459 /* Assert VMEbus IRQ */
460 tmp = tmp | TSI148_LCSR_VICR_IRQL[level];
Martyn Welch29848ac2010-02-18 15:13:05 +0000461 iowrite32be(tmp, bridge->base + TSI148_LCSR_VICR);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100462
463 /* XXX Consider implementing a timeout? */
Martyn Welch29848ac2010-02-18 15:13:05 +0000464 wait_event_interruptible(bridge->iack_queue,
465 tsi148_iack_received(bridge));
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100466
Emilio G. Cota886953e2010-11-12 11:14:07 +0000467 mutex_unlock(&bridge->vme_int);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100468
469 return 0;
470}
471
472/*
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100473 * Initialize a slave window with the requested attributes.
474 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +0000475static int tsi148_slave_set(struct vme_slave_resource *image, int enabled,
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100476 unsigned long long vme_base, unsigned long long size,
Martyn Welch6af04b02011-12-01 17:06:29 +0000477 dma_addr_t pci_base, u32 aspace, u32 cycle)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100478{
479 unsigned int i, addr = 0, granularity = 0;
480 unsigned int temp_ctl = 0;
481 unsigned int vme_base_low, vme_base_high;
482 unsigned int vme_bound_low, vme_bound_high;
483 unsigned int pci_offset_low, pci_offset_high;
484 unsigned long long vme_bound, pci_offset;
Martyn Welch48d93562010-03-22 14:58:50 +0000485 struct vme_bridge *tsi148_bridge;
Martyn Welch29848ac2010-02-18 15:13:05 +0000486 struct tsi148_driver *bridge;
487
Martyn Welch48d93562010-03-22 14:58:50 +0000488 tsi148_bridge = image->parent;
489 bridge = tsi148_bridge->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100490
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100491 i = image->number;
492
493 switch (aspace) {
494 case VME_A16:
495 granularity = 0x10;
496 addr |= TSI148_LCSR_ITAT_AS_A16;
497 break;
498 case VME_A24:
499 granularity = 0x1000;
500 addr |= TSI148_LCSR_ITAT_AS_A24;
501 break;
502 case VME_A32:
503 granularity = 0x10000;
504 addr |= TSI148_LCSR_ITAT_AS_A32;
505 break;
506 case VME_A64:
507 granularity = 0x10000;
508 addr |= TSI148_LCSR_ITAT_AS_A64;
509 break;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100510 default:
Martyn Welch48d93562010-03-22 14:58:50 +0000511 dev_err(tsi148_bridge->parent, "Invalid address space\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100512 return -EINVAL;
513 break;
514 }
515
516 /* Convert 64-bit variables to 2x 32-bit variables */
517 reg_split(vme_base, &vme_base_high, &vme_base_low);
518
519 /*
520 * Bound address is a valid address for the window, adjust
521 * accordingly
522 */
523 vme_bound = vme_base + size - granularity;
524 reg_split(vme_bound, &vme_bound_high, &vme_bound_low);
525 pci_offset = (unsigned long long)pci_base - vme_base;
526 reg_split(pci_offset, &pci_offset_high, &pci_offset_low);
527
528 if (vme_base_low & (granularity - 1)) {
Martyn Welch48d93562010-03-22 14:58:50 +0000529 dev_err(tsi148_bridge->parent, "Invalid VME base alignment\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100530 return -EINVAL;
531 }
532 if (vme_bound_low & (granularity - 1)) {
Martyn Welch48d93562010-03-22 14:58:50 +0000533 dev_err(tsi148_bridge->parent, "Invalid VME bound alignment\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100534 return -EINVAL;
535 }
536 if (pci_offset_low & (granularity - 1)) {
Martyn Welch48d93562010-03-22 14:58:50 +0000537 dev_err(tsi148_bridge->parent, "Invalid PCI Offset "
538 "alignment\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100539 return -EINVAL;
540 }
541
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100542 /* Disable while we are mucking around */
Martyn Welch29848ac2010-02-18 15:13:05 +0000543 temp_ctl = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100544 TSI148_LCSR_OFFSET_ITAT);
545 temp_ctl &= ~TSI148_LCSR_ITAT_EN;
Martyn Welch29848ac2010-02-18 15:13:05 +0000546 iowrite32be(temp_ctl, bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100547 TSI148_LCSR_OFFSET_ITAT);
548
549 /* Setup mapping */
Martyn Welch29848ac2010-02-18 15:13:05 +0000550 iowrite32be(vme_base_high, bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100551 TSI148_LCSR_OFFSET_ITSAU);
Martyn Welch29848ac2010-02-18 15:13:05 +0000552 iowrite32be(vme_base_low, bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100553 TSI148_LCSR_OFFSET_ITSAL);
Martyn Welch29848ac2010-02-18 15:13:05 +0000554 iowrite32be(vme_bound_high, bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100555 TSI148_LCSR_OFFSET_ITEAU);
Martyn Welch29848ac2010-02-18 15:13:05 +0000556 iowrite32be(vme_bound_low, bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100557 TSI148_LCSR_OFFSET_ITEAL);
Martyn Welch29848ac2010-02-18 15:13:05 +0000558 iowrite32be(pci_offset_high, bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100559 TSI148_LCSR_OFFSET_ITOFU);
Martyn Welch29848ac2010-02-18 15:13:05 +0000560 iowrite32be(pci_offset_low, bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100561 TSI148_LCSR_OFFSET_ITOFL);
562
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100563 /* Setup 2eSST speeds */
564 temp_ctl &= ~TSI148_LCSR_ITAT_2eSSTM_M;
565 switch (cycle & (VME_2eSST160 | VME_2eSST267 | VME_2eSST320)) {
566 case VME_2eSST160:
567 temp_ctl |= TSI148_LCSR_ITAT_2eSSTM_160;
568 break;
569 case VME_2eSST267:
570 temp_ctl |= TSI148_LCSR_ITAT_2eSSTM_267;
571 break;
572 case VME_2eSST320:
573 temp_ctl |= TSI148_LCSR_ITAT_2eSSTM_320;
574 break;
575 }
576
577 /* Setup cycle types */
578 temp_ctl &= ~(0x1F << 7);
579 if (cycle & VME_BLT)
580 temp_ctl |= TSI148_LCSR_ITAT_BLT;
581 if (cycle & VME_MBLT)
582 temp_ctl |= TSI148_LCSR_ITAT_MBLT;
583 if (cycle & VME_2eVME)
584 temp_ctl |= TSI148_LCSR_ITAT_2eVME;
585 if (cycle & VME_2eSST)
586 temp_ctl |= TSI148_LCSR_ITAT_2eSST;
587 if (cycle & VME_2eSSTB)
588 temp_ctl |= TSI148_LCSR_ITAT_2eSSTB;
589
590 /* Setup address space */
591 temp_ctl &= ~TSI148_LCSR_ITAT_AS_M;
592 temp_ctl |= addr;
593
594 temp_ctl &= ~0xF;
595 if (cycle & VME_SUPER)
596 temp_ctl |= TSI148_LCSR_ITAT_SUPR ;
597 if (cycle & VME_USER)
598 temp_ctl |= TSI148_LCSR_ITAT_NPRIV;
599 if (cycle & VME_PROG)
600 temp_ctl |= TSI148_LCSR_ITAT_PGM;
601 if (cycle & VME_DATA)
602 temp_ctl |= TSI148_LCSR_ITAT_DATA;
603
604 /* Write ctl reg without enable */
Martyn Welch29848ac2010-02-18 15:13:05 +0000605 iowrite32be(temp_ctl, bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100606 TSI148_LCSR_OFFSET_ITAT);
607
608 if (enabled)
609 temp_ctl |= TSI148_LCSR_ITAT_EN;
610
Martyn Welch29848ac2010-02-18 15:13:05 +0000611 iowrite32be(temp_ctl, bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100612 TSI148_LCSR_OFFSET_ITAT);
613
614 return 0;
615}
616
617/*
618 * Get slave window configuration.
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100619 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +0000620static int tsi148_slave_get(struct vme_slave_resource *image, int *enabled,
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100621 unsigned long long *vme_base, unsigned long long *size,
Martyn Welch6af04b02011-12-01 17:06:29 +0000622 dma_addr_t *pci_base, u32 *aspace, u32 *cycle)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100623{
624 unsigned int i, granularity = 0, ctl = 0;
625 unsigned int vme_base_low, vme_base_high;
626 unsigned int vme_bound_low, vme_bound_high;
627 unsigned int pci_offset_low, pci_offset_high;
628 unsigned long long vme_bound, pci_offset;
Martyn Welch29848ac2010-02-18 15:13:05 +0000629 struct tsi148_driver *bridge;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100630
Martyn Welch29848ac2010-02-18 15:13:05 +0000631 bridge = image->parent->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100632
633 i = image->number;
634
635 /* Read registers */
Martyn Welch29848ac2010-02-18 15:13:05 +0000636 ctl = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100637 TSI148_LCSR_OFFSET_ITAT);
638
Martyn Welch29848ac2010-02-18 15:13:05 +0000639 vme_base_high = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100640 TSI148_LCSR_OFFSET_ITSAU);
Martyn Welch29848ac2010-02-18 15:13:05 +0000641 vme_base_low = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100642 TSI148_LCSR_OFFSET_ITSAL);
Martyn Welch29848ac2010-02-18 15:13:05 +0000643 vme_bound_high = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100644 TSI148_LCSR_OFFSET_ITEAU);
Martyn Welch29848ac2010-02-18 15:13:05 +0000645 vme_bound_low = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100646 TSI148_LCSR_OFFSET_ITEAL);
Martyn Welch29848ac2010-02-18 15:13:05 +0000647 pci_offset_high = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100648 TSI148_LCSR_OFFSET_ITOFU);
Martyn Welch29848ac2010-02-18 15:13:05 +0000649 pci_offset_low = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100650 TSI148_LCSR_OFFSET_ITOFL);
651
652 /* Convert 64-bit variables to 2x 32-bit variables */
653 reg_join(vme_base_high, vme_base_low, vme_base);
654 reg_join(vme_bound_high, vme_bound_low, &vme_bound);
655 reg_join(pci_offset_high, pci_offset_low, &pci_offset);
656
Joe Schultz098ced82014-04-03 14:47:55 -0500657 *pci_base = (dma_addr_t)(*vme_base + pci_offset);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100658
659 *enabled = 0;
660 *aspace = 0;
661 *cycle = 0;
662
663 if (ctl & TSI148_LCSR_ITAT_EN)
664 *enabled = 1;
665
666 if ((ctl & TSI148_LCSR_ITAT_AS_M) == TSI148_LCSR_ITAT_AS_A16) {
667 granularity = 0x10;
668 *aspace |= VME_A16;
669 }
670 if ((ctl & TSI148_LCSR_ITAT_AS_M) == TSI148_LCSR_ITAT_AS_A24) {
671 granularity = 0x1000;
672 *aspace |= VME_A24;
673 }
674 if ((ctl & TSI148_LCSR_ITAT_AS_M) == TSI148_LCSR_ITAT_AS_A32) {
675 granularity = 0x10000;
676 *aspace |= VME_A32;
677 }
678 if ((ctl & TSI148_LCSR_ITAT_AS_M) == TSI148_LCSR_ITAT_AS_A64) {
679 granularity = 0x10000;
680 *aspace |= VME_A64;
681 }
682
683 /* Need granularity before we set the size */
684 *size = (unsigned long long)((vme_bound - *vme_base) + granularity);
685
686
687 if ((ctl & TSI148_LCSR_ITAT_2eSSTM_M) == TSI148_LCSR_ITAT_2eSSTM_160)
688 *cycle |= VME_2eSST160;
689 if ((ctl & TSI148_LCSR_ITAT_2eSSTM_M) == TSI148_LCSR_ITAT_2eSSTM_267)
690 *cycle |= VME_2eSST267;
691 if ((ctl & TSI148_LCSR_ITAT_2eSSTM_M) == TSI148_LCSR_ITAT_2eSSTM_320)
692 *cycle |= VME_2eSST320;
693
694 if (ctl & TSI148_LCSR_ITAT_BLT)
695 *cycle |= VME_BLT;
696 if (ctl & TSI148_LCSR_ITAT_MBLT)
697 *cycle |= VME_MBLT;
698 if (ctl & TSI148_LCSR_ITAT_2eVME)
699 *cycle |= VME_2eVME;
700 if (ctl & TSI148_LCSR_ITAT_2eSST)
701 *cycle |= VME_2eSST;
702 if (ctl & TSI148_LCSR_ITAT_2eSSTB)
703 *cycle |= VME_2eSSTB;
704
705 if (ctl & TSI148_LCSR_ITAT_SUPR)
706 *cycle |= VME_SUPER;
707 if (ctl & TSI148_LCSR_ITAT_NPRIV)
708 *cycle |= VME_USER;
709 if (ctl & TSI148_LCSR_ITAT_PGM)
710 *cycle |= VME_PROG;
711 if (ctl & TSI148_LCSR_ITAT_DATA)
712 *cycle |= VME_DATA;
713
714 return 0;
715}
716
717/*
718 * Allocate and map PCI Resource
719 */
720static int tsi148_alloc_resource(struct vme_master_resource *image,
721 unsigned long long size)
722{
723 unsigned long long existing_size;
724 int retval = 0;
725 struct pci_dev *pdev;
Martyn Welch29848ac2010-02-18 15:13:05 +0000726 struct vme_bridge *tsi148_bridge;
727
728 tsi148_bridge = image->parent;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100729
Aaron Sierra177581fa2014-04-03 14:48:27 -0500730 pdev = to_pci_dev(tsi148_bridge->parent);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100731
Martyn Welch8fafb472010-02-18 15:13:12 +0000732 existing_size = (unsigned long long)(image->bus_resource.end -
733 image->bus_resource.start);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100734
735 /* If the existing size is OK, return */
Martyn Welch59c22902009-10-29 16:35:01 +0000736 if ((size != 0) && (existing_size == (size - 1)))
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100737 return 0;
738
739 if (existing_size != 0) {
740 iounmap(image->kern_base);
741 image->kern_base = NULL;
Ilia Mirkin794a8942011-03-13 00:29:13 -0500742 kfree(image->bus_resource.name);
Emilio G. Cota886953e2010-11-12 11:14:07 +0000743 release_resource(&image->bus_resource);
Markus Elfring6d011dd2017-08-25 11:55:03 +0200744 memset(&image->bus_resource, 0, sizeof(image->bus_resource));
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100745 }
746
Martyn Welch59c22902009-10-29 16:35:01 +0000747 /* Exit here if size is zero */
Martyn Welch79463282010-03-22 14:58:57 +0000748 if (size == 0)
Martyn Welch59c22902009-10-29 16:35:01 +0000749 return 0;
Martyn Welch59c22902009-10-29 16:35:01 +0000750
Markus Elfringa75dc632017-08-25 12:00:17 +0200751 if (!image->bus_resource.name) {
Julia Lawall0aa3f132010-05-30 22:27:46 +0200752 image->bus_resource.name = kmalloc(VMENAMSIZ+3, GFP_ATOMIC);
Markus Elfringa75dc632017-08-25 12:00:17 +0200753 if (!image->bus_resource.name) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100754 retval = -ENOMEM;
755 goto err_name;
756 }
757 }
758
Martyn Welch8fafb472010-02-18 15:13:12 +0000759 sprintf((char *)image->bus_resource.name, "%s.%d", tsi148_bridge->name,
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100760 image->number);
761
Martyn Welch8fafb472010-02-18 15:13:12 +0000762 image->bus_resource.start = 0;
763 image->bus_resource.end = (unsigned long)size;
764 image->bus_resource.flags = IORESOURCE_MEM;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100765
766 retval = pci_bus_alloc_resource(pdev->bus,
Dmitry Kalinkinda5ae8a2015-07-08 17:42:17 +0300767 &image->bus_resource, size, 0x10000, PCIBIOS_MIN_MEM,
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100768 0, NULL, NULL);
769 if (retval) {
Martyn Welch48d93562010-03-22 14:58:50 +0000770 dev_err(tsi148_bridge->parent, "Failed to allocate mem "
771 "resource for window %d size 0x%lx start 0x%lx\n",
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100772 image->number, (unsigned long)size,
Martyn Welch8fafb472010-02-18 15:13:12 +0000773 (unsigned long)image->bus_resource.start);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100774 goto err_resource;
775 }
776
777 image->kern_base = ioremap_nocache(
Martyn Welch8fafb472010-02-18 15:13:12 +0000778 image->bus_resource.start, size);
Markus Elfringa75dc632017-08-25 12:00:17 +0200779 if (!image->kern_base) {
Martyn Welch48d93562010-03-22 14:58:50 +0000780 dev_err(tsi148_bridge->parent, "Failed to remap resource\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100781 retval = -ENOMEM;
782 goto err_remap;
783 }
784
785 return 0;
786
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100787err_remap:
Emilio G. Cota886953e2010-11-12 11:14:07 +0000788 release_resource(&image->bus_resource);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100789err_resource:
Martyn Welch8fafb472010-02-18 15:13:12 +0000790 kfree(image->bus_resource.name);
Markus Elfring6d011dd2017-08-25 11:55:03 +0200791 memset(&image->bus_resource, 0, sizeof(image->bus_resource));
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100792err_name:
793 return retval;
794}
795
796/*
797 * Free and unmap PCI Resource
798 */
799static void tsi148_free_resource(struct vme_master_resource *image)
800{
801 iounmap(image->kern_base);
802 image->kern_base = NULL;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000803 release_resource(&image->bus_resource);
Martyn Welch8fafb472010-02-18 15:13:12 +0000804 kfree(image->bus_resource.name);
Markus Elfring6d011dd2017-08-25 11:55:03 +0200805 memset(&image->bus_resource, 0, sizeof(image->bus_resource));
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100806}
807
808/*
809 * Set the attributes of an outbound window.
810 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +0000811static int tsi148_master_set(struct vme_master_resource *image, int enabled,
Martyn Welch6af04b02011-12-01 17:06:29 +0000812 unsigned long long vme_base, unsigned long long size, u32 aspace,
813 u32 cycle, u32 dwidth)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100814{
815 int retval = 0;
816 unsigned int i;
817 unsigned int temp_ctl = 0;
818 unsigned int pci_base_low, pci_base_high;
819 unsigned int pci_bound_low, pci_bound_high;
820 unsigned int vme_offset_low, vme_offset_high;
821 unsigned long long pci_bound, vme_offset, pci_base;
Martyn Welch48d93562010-03-22 14:58:50 +0000822 struct vme_bridge *tsi148_bridge;
Martyn Welch29848ac2010-02-18 15:13:05 +0000823 struct tsi148_driver *bridge;
Joe Schultz226572b2014-04-03 14:48:16 -0500824 struct pci_bus_region region;
825 struct pci_dev *pdev;
Martyn Welch29848ac2010-02-18 15:13:05 +0000826
Martyn Welch48d93562010-03-22 14:58:50 +0000827 tsi148_bridge = image->parent;
828
829 bridge = tsi148_bridge->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100830
Aaron Sierra177581fa2014-04-03 14:48:27 -0500831 pdev = to_pci_dev(tsi148_bridge->parent);
Joe Schultz226572b2014-04-03 14:48:16 -0500832
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100833 /* Verify input data */
834 if (vme_base & 0xFFFF) {
Martyn Welch48d93562010-03-22 14:58:50 +0000835 dev_err(tsi148_bridge->parent, "Invalid VME Window "
836 "alignment\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100837 retval = -EINVAL;
838 goto err_window;
839 }
Martyn Welch59c22902009-10-29 16:35:01 +0000840
841 if ((size == 0) && (enabled != 0)) {
Martyn Welch48d93562010-03-22 14:58:50 +0000842 dev_err(tsi148_bridge->parent, "Size must be non-zero for "
843 "enabled windows\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100844 retval = -EINVAL;
845 goto err_window;
846 }
847
Emilio G. Cota886953e2010-11-12 11:14:07 +0000848 spin_lock(&image->lock);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100849
850 /* Let's allocate the resource here rather than further up the stack as
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300851 * it avoids pushing loads of bus dependent stuff up the stack. If size
Martyn Welch59c22902009-10-29 16:35:01 +0000852 * is zero, any existing resource will be freed.
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100853 */
854 retval = tsi148_alloc_resource(image, size);
855 if (retval) {
Emilio G. Cota886953e2010-11-12 11:14:07 +0000856 spin_unlock(&image->lock);
Martyn Welch48d93562010-03-22 14:58:50 +0000857 dev_err(tsi148_bridge->parent, "Unable to allocate memory for "
Martyn Welch59c22902009-10-29 16:35:01 +0000858 "resource\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100859 goto err_res;
860 }
861
Martyn Welch59c22902009-10-29 16:35:01 +0000862 if (size == 0) {
863 pci_base = 0;
864 pci_bound = 0;
865 vme_offset = 0;
866 } else {
Joe Schultz226572b2014-04-03 14:48:16 -0500867 pcibios_resource_to_bus(pdev->bus, &region,
868 &image->bus_resource);
869 pci_base = region.start;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100870
Martyn Welch59c22902009-10-29 16:35:01 +0000871 /*
872 * Bound address is a valid address for the window, adjust
873 * according to window granularity.
874 */
875 pci_bound = pci_base + (size - 0x10000);
876 vme_offset = vme_base - pci_base;
877 }
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100878
879 /* Convert 64-bit variables to 2x 32-bit variables */
880 reg_split(pci_base, &pci_base_high, &pci_base_low);
881 reg_split(pci_bound, &pci_bound_high, &pci_bound_low);
882 reg_split(vme_offset, &vme_offset_high, &vme_offset_low);
883
884 if (pci_base_low & 0xFFFF) {
Emilio G. Cota886953e2010-11-12 11:14:07 +0000885 spin_unlock(&image->lock);
Martyn Welch48d93562010-03-22 14:58:50 +0000886 dev_err(tsi148_bridge->parent, "Invalid PCI base alignment\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100887 retval = -EINVAL;
888 goto err_gran;
889 }
890 if (pci_bound_low & 0xFFFF) {
Emilio G. Cota886953e2010-11-12 11:14:07 +0000891 spin_unlock(&image->lock);
Martyn Welch48d93562010-03-22 14:58:50 +0000892 dev_err(tsi148_bridge->parent, "Invalid PCI bound alignment\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100893 retval = -EINVAL;
894 goto err_gran;
895 }
896 if (vme_offset_low & 0xFFFF) {
Emilio G. Cota886953e2010-11-12 11:14:07 +0000897 spin_unlock(&image->lock);
Martyn Welch48d93562010-03-22 14:58:50 +0000898 dev_err(tsi148_bridge->parent, "Invalid VME Offset "
899 "alignment\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100900 retval = -EINVAL;
901 goto err_gran;
902 }
903
904 i = image->number;
905
906 /* Disable while we are mucking around */
Martyn Welch29848ac2010-02-18 15:13:05 +0000907 temp_ctl = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100908 TSI148_LCSR_OFFSET_OTAT);
909 temp_ctl &= ~TSI148_LCSR_OTAT_EN;
Martyn Welch29848ac2010-02-18 15:13:05 +0000910 iowrite32be(temp_ctl, bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100911 TSI148_LCSR_OFFSET_OTAT);
912
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100913 /* Setup 2eSST speeds */
914 temp_ctl &= ~TSI148_LCSR_OTAT_2eSSTM_M;
915 switch (cycle & (VME_2eSST160 | VME_2eSST267 | VME_2eSST320)) {
916 case VME_2eSST160:
917 temp_ctl |= TSI148_LCSR_OTAT_2eSSTM_160;
918 break;
919 case VME_2eSST267:
920 temp_ctl |= TSI148_LCSR_OTAT_2eSSTM_267;
921 break;
922 case VME_2eSST320:
923 temp_ctl |= TSI148_LCSR_OTAT_2eSSTM_320;
924 break;
925 }
926
927 /* Setup cycle types */
928 if (cycle & VME_BLT) {
929 temp_ctl &= ~TSI148_LCSR_OTAT_TM_M;
930 temp_ctl |= TSI148_LCSR_OTAT_TM_BLT;
931 }
932 if (cycle & VME_MBLT) {
933 temp_ctl &= ~TSI148_LCSR_OTAT_TM_M;
934 temp_ctl |= TSI148_LCSR_OTAT_TM_MBLT;
935 }
936 if (cycle & VME_2eVME) {
937 temp_ctl &= ~TSI148_LCSR_OTAT_TM_M;
938 temp_ctl |= TSI148_LCSR_OTAT_TM_2eVME;
939 }
940 if (cycle & VME_2eSST) {
941 temp_ctl &= ~TSI148_LCSR_OTAT_TM_M;
942 temp_ctl |= TSI148_LCSR_OTAT_TM_2eSST;
943 }
944 if (cycle & VME_2eSSTB) {
Martyn Welch48d93562010-03-22 14:58:50 +0000945 dev_warn(tsi148_bridge->parent, "Currently not setting "
946 "Broadcast Select Registers\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100947 temp_ctl &= ~TSI148_LCSR_OTAT_TM_M;
948 temp_ctl |= TSI148_LCSR_OTAT_TM_2eSSTB;
949 }
950
951 /* Setup data width */
952 temp_ctl &= ~TSI148_LCSR_OTAT_DBW_M;
953 switch (dwidth) {
954 case VME_D16:
955 temp_ctl |= TSI148_LCSR_OTAT_DBW_16;
956 break;
957 case VME_D32:
958 temp_ctl |= TSI148_LCSR_OTAT_DBW_32;
959 break;
960 default:
Emilio G. Cota886953e2010-11-12 11:14:07 +0000961 spin_unlock(&image->lock);
Martyn Welch48d93562010-03-22 14:58:50 +0000962 dev_err(tsi148_bridge->parent, "Invalid data width\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100963 retval = -EINVAL;
964 goto err_dwidth;
965 }
966
967 /* Setup address space */
968 temp_ctl &= ~TSI148_LCSR_OTAT_AMODE_M;
969 switch (aspace) {
970 case VME_A16:
971 temp_ctl |= TSI148_LCSR_OTAT_AMODE_A16;
972 break;
973 case VME_A24:
974 temp_ctl |= TSI148_LCSR_OTAT_AMODE_A24;
975 break;
976 case VME_A32:
977 temp_ctl |= TSI148_LCSR_OTAT_AMODE_A32;
978 break;
979 case VME_A64:
980 temp_ctl |= TSI148_LCSR_OTAT_AMODE_A64;
981 break;
982 case VME_CRCSR:
983 temp_ctl |= TSI148_LCSR_OTAT_AMODE_CRCSR;
984 break;
985 case VME_USER1:
986 temp_ctl |= TSI148_LCSR_OTAT_AMODE_USER1;
987 break;
988 case VME_USER2:
989 temp_ctl |= TSI148_LCSR_OTAT_AMODE_USER2;
990 break;
991 case VME_USER3:
992 temp_ctl |= TSI148_LCSR_OTAT_AMODE_USER3;
993 break;
994 case VME_USER4:
995 temp_ctl |= TSI148_LCSR_OTAT_AMODE_USER4;
996 break;
997 default:
Emilio G. Cota886953e2010-11-12 11:14:07 +0000998 spin_unlock(&image->lock);
Martyn Welch48d93562010-03-22 14:58:50 +0000999 dev_err(tsi148_bridge->parent, "Invalid address space\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001000 retval = -EINVAL;
1001 goto err_aspace;
1002 break;
1003 }
1004
1005 temp_ctl &= ~(3<<4);
1006 if (cycle & VME_SUPER)
1007 temp_ctl |= TSI148_LCSR_OTAT_SUP;
1008 if (cycle & VME_PROG)
1009 temp_ctl |= TSI148_LCSR_OTAT_PGM;
1010
1011 /* Setup mapping */
Martyn Welch29848ac2010-02-18 15:13:05 +00001012 iowrite32be(pci_base_high, bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001013 TSI148_LCSR_OFFSET_OTSAU);
Martyn Welch29848ac2010-02-18 15:13:05 +00001014 iowrite32be(pci_base_low, bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001015 TSI148_LCSR_OFFSET_OTSAL);
Martyn Welch29848ac2010-02-18 15:13:05 +00001016 iowrite32be(pci_bound_high, bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001017 TSI148_LCSR_OFFSET_OTEAU);
Martyn Welch29848ac2010-02-18 15:13:05 +00001018 iowrite32be(pci_bound_low, bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001019 TSI148_LCSR_OFFSET_OTEAL);
Martyn Welch29848ac2010-02-18 15:13:05 +00001020 iowrite32be(vme_offset_high, bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001021 TSI148_LCSR_OFFSET_OTOFU);
Martyn Welch29848ac2010-02-18 15:13:05 +00001022 iowrite32be(vme_offset_low, bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001023 TSI148_LCSR_OFFSET_OTOFL);
1024
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001025 /* Write ctl reg without enable */
Martyn Welch29848ac2010-02-18 15:13:05 +00001026 iowrite32be(temp_ctl, bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001027 TSI148_LCSR_OFFSET_OTAT);
1028
1029 if (enabled)
1030 temp_ctl |= TSI148_LCSR_OTAT_EN;
1031
Martyn Welch29848ac2010-02-18 15:13:05 +00001032 iowrite32be(temp_ctl, bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001033 TSI148_LCSR_OFFSET_OTAT);
1034
Emilio G. Cota886953e2010-11-12 11:14:07 +00001035 spin_unlock(&image->lock);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001036 return 0;
1037
1038err_aspace:
1039err_dwidth:
1040err_gran:
1041 tsi148_free_resource(image);
1042err_res:
1043err_window:
1044 return retval;
1045
1046}
1047
1048/*
1049 * Set the attributes of an outbound window.
1050 *
1051 * XXX Not parsing prefetch information.
1052 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00001053static int __tsi148_master_get(struct vme_master_resource *image, int *enabled,
Martyn Welch6af04b02011-12-01 17:06:29 +00001054 unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
1055 u32 *cycle, u32 *dwidth)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001056{
1057 unsigned int i, ctl;
1058 unsigned int pci_base_low, pci_base_high;
1059 unsigned int pci_bound_low, pci_bound_high;
1060 unsigned int vme_offset_low, vme_offset_high;
1061
1062 unsigned long long pci_base, pci_bound, vme_offset;
Martyn Welch29848ac2010-02-18 15:13:05 +00001063 struct tsi148_driver *bridge;
1064
1065 bridge = image->parent->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001066
1067 i = image->number;
1068
Martyn Welch29848ac2010-02-18 15:13:05 +00001069 ctl = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001070 TSI148_LCSR_OFFSET_OTAT);
1071
Martyn Welch29848ac2010-02-18 15:13:05 +00001072 pci_base_high = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001073 TSI148_LCSR_OFFSET_OTSAU);
Martyn Welch29848ac2010-02-18 15:13:05 +00001074 pci_base_low = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001075 TSI148_LCSR_OFFSET_OTSAL);
Martyn Welch29848ac2010-02-18 15:13:05 +00001076 pci_bound_high = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001077 TSI148_LCSR_OFFSET_OTEAU);
Martyn Welch29848ac2010-02-18 15:13:05 +00001078 pci_bound_low = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001079 TSI148_LCSR_OFFSET_OTEAL);
Martyn Welch29848ac2010-02-18 15:13:05 +00001080 vme_offset_high = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001081 TSI148_LCSR_OFFSET_OTOFU);
Martyn Welch29848ac2010-02-18 15:13:05 +00001082 vme_offset_low = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001083 TSI148_LCSR_OFFSET_OTOFL);
1084
1085 /* Convert 64-bit variables to 2x 32-bit variables */
1086 reg_join(pci_base_high, pci_base_low, &pci_base);
1087 reg_join(pci_bound_high, pci_bound_low, &pci_bound);
1088 reg_join(vme_offset_high, vme_offset_low, &vme_offset);
1089
1090 *vme_base = pci_base + vme_offset;
1091 *size = (unsigned long long)(pci_bound - pci_base) + 0x10000;
1092
1093 *enabled = 0;
1094 *aspace = 0;
1095 *cycle = 0;
1096 *dwidth = 0;
1097
1098 if (ctl & TSI148_LCSR_OTAT_EN)
1099 *enabled = 1;
1100
1101 /* Setup address space */
1102 if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_A16)
1103 *aspace |= VME_A16;
1104 if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_A24)
1105 *aspace |= VME_A24;
1106 if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_A32)
1107 *aspace |= VME_A32;
1108 if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_A64)
1109 *aspace |= VME_A64;
1110 if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_CRCSR)
1111 *aspace |= VME_CRCSR;
1112 if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_USER1)
1113 *aspace |= VME_USER1;
1114 if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_USER2)
1115 *aspace |= VME_USER2;
1116 if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_USER3)
1117 *aspace |= VME_USER3;
1118 if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_USER4)
1119 *aspace |= VME_USER4;
1120
1121 /* Setup 2eSST speeds */
1122 if ((ctl & TSI148_LCSR_OTAT_2eSSTM_M) == TSI148_LCSR_OTAT_2eSSTM_160)
1123 *cycle |= VME_2eSST160;
1124 if ((ctl & TSI148_LCSR_OTAT_2eSSTM_M) == TSI148_LCSR_OTAT_2eSSTM_267)
1125 *cycle |= VME_2eSST267;
1126 if ((ctl & TSI148_LCSR_OTAT_2eSSTM_M) == TSI148_LCSR_OTAT_2eSSTM_320)
1127 *cycle |= VME_2eSST320;
1128
1129 /* Setup cycle types */
Martyn Welch79463282010-03-22 14:58:57 +00001130 if ((ctl & TSI148_LCSR_OTAT_TM_M) == TSI148_LCSR_OTAT_TM_SCT)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001131 *cycle |= VME_SCT;
Martyn Welch79463282010-03-22 14:58:57 +00001132 if ((ctl & TSI148_LCSR_OTAT_TM_M) == TSI148_LCSR_OTAT_TM_BLT)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001133 *cycle |= VME_BLT;
Martyn Welch79463282010-03-22 14:58:57 +00001134 if ((ctl & TSI148_LCSR_OTAT_TM_M) == TSI148_LCSR_OTAT_TM_MBLT)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001135 *cycle |= VME_MBLT;
Martyn Welch79463282010-03-22 14:58:57 +00001136 if ((ctl & TSI148_LCSR_OTAT_TM_M) == TSI148_LCSR_OTAT_TM_2eVME)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001137 *cycle |= VME_2eVME;
Martyn Welch79463282010-03-22 14:58:57 +00001138 if ((ctl & TSI148_LCSR_OTAT_TM_M) == TSI148_LCSR_OTAT_TM_2eSST)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001139 *cycle |= VME_2eSST;
Martyn Welch79463282010-03-22 14:58:57 +00001140 if ((ctl & TSI148_LCSR_OTAT_TM_M) == TSI148_LCSR_OTAT_TM_2eSSTB)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001141 *cycle |= VME_2eSSTB;
1142
1143 if (ctl & TSI148_LCSR_OTAT_SUP)
1144 *cycle |= VME_SUPER;
1145 else
1146 *cycle |= VME_USER;
1147
1148 if (ctl & TSI148_LCSR_OTAT_PGM)
1149 *cycle |= VME_PROG;
1150 else
1151 *cycle |= VME_DATA;
1152
1153 /* Setup data width */
1154 if ((ctl & TSI148_LCSR_OTAT_DBW_M) == TSI148_LCSR_OTAT_DBW_16)
1155 *dwidth = VME_D16;
1156 if ((ctl & TSI148_LCSR_OTAT_DBW_M) == TSI148_LCSR_OTAT_DBW_32)
1157 *dwidth = VME_D32;
1158
1159 return 0;
1160}
1161
1162
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00001163static int tsi148_master_get(struct vme_master_resource *image, int *enabled,
Martyn Welch6af04b02011-12-01 17:06:29 +00001164 unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
1165 u32 *cycle, u32 *dwidth)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001166{
1167 int retval;
1168
Emilio G. Cota886953e2010-11-12 11:14:07 +00001169 spin_lock(&image->lock);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001170
1171 retval = __tsi148_master_get(image, enabled, vme_base, size, aspace,
1172 cycle, dwidth);
1173
Emilio G. Cota886953e2010-11-12 11:14:07 +00001174 spin_unlock(&image->lock);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001175
1176 return retval;
1177}
1178
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00001179static ssize_t tsi148_master_read(struct vme_master_resource *image, void *buf,
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001180 size_t count, loff_t offset)
1181{
1182 int retval, enabled;
1183 unsigned long long vme_base, size;
Martyn Welch6af04b02011-12-01 17:06:29 +00001184 u32 aspace, cycle, dwidth;
Dmitry Kalinkind3337eb2015-10-05 06:59:17 +03001185 struct vme_error_handler *handler = NULL;
Martyn Welch29848ac2010-02-18 15:13:05 +00001186 struct vme_bridge *tsi148_bridge;
Jingoo Han4e8764d2013-08-19 16:40:15 +09001187 void __iomem *addr = image->kern_base + offset;
Martyn Welch363e2e62012-07-19 17:48:46 +01001188 unsigned int done = 0;
1189 unsigned int count32;
Martyn Welch29848ac2010-02-18 15:13:05 +00001190
1191 tsi148_bridge = image->parent;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001192
Emilio G. Cota886953e2010-11-12 11:14:07 +00001193 spin_lock(&image->lock);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001194
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001195 if (err_chk) {
1196 __tsi148_master_get(image, &enabled, &vme_base, &size, &aspace,
1197 &cycle, &dwidth);
1198 handler = vme_register_error_handler(tsi148_bridge, aspace,
1199 vme_base + offset, count);
1200 if (!handler) {
1201 spin_unlock(&image->lock);
1202 return -ENOMEM;
1203 }
1204 }
1205
Martyn Welch363e2e62012-07-19 17:48:46 +01001206 /* The following code handles VME address alignment. We cannot use
Martyn Welcha2a720e2014-02-06 13:35:36 +00001207 * memcpy_xxx here because it may cut data transfers in to 8-bit
1208 * cycles when D16 or D32 cycles are required on the VME bus.
Martyn Welch363e2e62012-07-19 17:48:46 +01001209 * On the other hand, the bridge itself assures that the maximum data
1210 * cycle configured for the transfer is used and splits it
1211 * automatically for non-aligned addresses, so we don't want the
1212 * overhead of needlessly forcing small transfers for the entire cycle.
1213 */
1214 if ((uintptr_t)addr & 0x1) {
1215 *(u8 *)buf = ioread8(addr);
1216 done += 1;
1217 if (done == count)
1218 goto out;
1219 }
Martyn Welchf0342e62014-02-07 15:48:56 +00001220 if ((uintptr_t)(addr + done) & 0x2) {
Martyn Welch363e2e62012-07-19 17:48:46 +01001221 if ((count - done) < 2) {
1222 *(u8 *)(buf + done) = ioread8(addr + done);
1223 done += 1;
1224 goto out;
1225 } else {
1226 *(u16 *)(buf + done) = ioread16(addr + done);
1227 done += 2;
1228 }
1229 }
1230
1231 count32 = (count - done) & ~0x3;
Martyn Welcha2a720e2014-02-06 13:35:36 +00001232 while (done < count32) {
1233 *(u32 *)(buf + done) = ioread32(addr + done);
1234 done += 4;
Martyn Welch363e2e62012-07-19 17:48:46 +01001235 }
1236
1237 if ((count - done) & 0x2) {
1238 *(u16 *)(buf + done) = ioread16(addr + done);
1239 done += 2;
1240 }
1241 if ((count - done) & 0x1) {
1242 *(u8 *)(buf + done) = ioread8(addr + done);
1243 done += 1;
1244 }
1245
1246out:
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001247 retval = count;
1248
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001249 if (err_chk) {
1250 if (handler->num_errors) {
1251 dev_err(image->parent->parent,
1252 "First VME read error detected an at address 0x%llx\n",
1253 handler->first_error);
1254 retval = handler->first_error - (vme_base + offset);
1255 }
1256 vme_unregister_error_handler(handler);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001257 }
1258
Emilio G. Cota886953e2010-11-12 11:14:07 +00001259 spin_unlock(&image->lock);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001260
1261 return retval;
1262}
1263
1264
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00001265static ssize_t tsi148_master_write(struct vme_master_resource *image, void *buf,
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001266 size_t count, loff_t offset)
1267{
1268 int retval = 0, enabled;
1269 unsigned long long vme_base, size;
Martyn Welch6af04b02011-12-01 17:06:29 +00001270 u32 aspace, cycle, dwidth;
Jingoo Han4e8764d2013-08-19 16:40:15 +09001271 void __iomem *addr = image->kern_base + offset;
Martyn Welch363e2e62012-07-19 17:48:46 +01001272 unsigned int done = 0;
1273 unsigned int count32;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001274
Dmitry Kalinkind3337eb2015-10-05 06:59:17 +03001275 struct vme_error_handler *handler = NULL;
Martyn Welch29848ac2010-02-18 15:13:05 +00001276 struct vme_bridge *tsi148_bridge;
1277 struct tsi148_driver *bridge;
1278
1279 tsi148_bridge = image->parent;
1280
1281 bridge = tsi148_bridge->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001282
Emilio G. Cota886953e2010-11-12 11:14:07 +00001283 spin_lock(&image->lock);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001284
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001285 if (err_chk) {
1286 __tsi148_master_get(image, &enabled, &vme_base, &size, &aspace,
1287 &cycle, &dwidth);
1288 handler = vme_register_error_handler(tsi148_bridge, aspace,
1289 vme_base + offset, count);
1290 if (!handler) {
1291 spin_unlock(&image->lock);
1292 return -ENOMEM;
1293 }
1294 }
1295
Martyn Welch363e2e62012-07-19 17:48:46 +01001296 /* Here we apply for the same strategy we do in master_read
Martyn Welcha2a720e2014-02-06 13:35:36 +00001297 * function in order to assure the correct cycles.
Martyn Welch363e2e62012-07-19 17:48:46 +01001298 */
1299 if ((uintptr_t)addr & 0x1) {
1300 iowrite8(*(u8 *)buf, addr);
1301 done += 1;
1302 if (done == count)
1303 goto out;
1304 }
Martyn Welchf0342e62014-02-07 15:48:56 +00001305 if ((uintptr_t)(addr + done) & 0x2) {
Martyn Welch363e2e62012-07-19 17:48:46 +01001306 if ((count - done) < 2) {
1307 iowrite8(*(u8 *)(buf + done), addr + done);
1308 done += 1;
1309 goto out;
1310 } else {
1311 iowrite16(*(u16 *)(buf + done), addr + done);
1312 done += 2;
1313 }
1314 }
1315
1316 count32 = (count - done) & ~0x3;
Martyn Welcha2a720e2014-02-06 13:35:36 +00001317 while (done < count32) {
1318 iowrite32(*(u32 *)(buf + done), addr + done);
1319 done += 4;
Martyn Welch363e2e62012-07-19 17:48:46 +01001320 }
1321
1322 if ((count - done) & 0x2) {
1323 iowrite16(*(u16 *)(buf + done), addr + done);
1324 done += 2;
1325 }
1326 if ((count - done) & 0x1) {
1327 iowrite8(*(u8 *)(buf + done), addr + done);
1328 done += 1;
1329 }
1330
1331out:
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001332 retval = count;
1333
1334 /*
1335 * Writes are posted. We need to do a read on the VME bus to flush out
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001336 * all of the writes before we check for errors. We can't guarantee
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001337 * that reading the data we have just written is safe. It is believed
1338 * that there isn't any read, write re-ordering, so we can read any
1339 * location in VME space, so lets read the Device ID from the tsi148's
1340 * own registers as mapped into CR/CSR space.
1341 *
1342 * We check for saved errors in the written address range/space.
1343 */
1344
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001345 if (err_chk) {
1346 ioread16(bridge->flush_image->kern_base + 0x7F000);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001347
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001348 if (handler->num_errors) {
1349 dev_warn(tsi148_bridge->parent,
1350 "First VME write error detected an at address 0x%llx\n",
1351 handler->first_error);
1352 retval = handler->first_error - (vme_base + offset);
1353 }
1354 vme_unregister_error_handler(handler);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001355 }
1356
Emilio G. Cota886953e2010-11-12 11:14:07 +00001357 spin_unlock(&image->lock);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001358
1359 return retval;
1360}
1361
1362/*
1363 * Perform an RMW cycle on the VME bus.
1364 *
1365 * Requires a previously configured master window, returns final value.
1366 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00001367static unsigned int tsi148_master_rmw(struct vme_master_resource *image,
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001368 unsigned int mask, unsigned int compare, unsigned int swap,
1369 loff_t offset)
1370{
1371 unsigned long long pci_addr;
1372 unsigned int pci_addr_high, pci_addr_low;
1373 u32 tmp, result;
1374 int i;
Martyn Welch29848ac2010-02-18 15:13:05 +00001375 struct tsi148_driver *bridge;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001376
Martyn Welch29848ac2010-02-18 15:13:05 +00001377 bridge = image->parent->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001378
1379 /* Find the PCI address that maps to the desired VME address */
1380 i = image->number;
1381
1382 /* Locking as we can only do one of these at a time */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001383 mutex_lock(&bridge->vme_rmw);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001384
1385 /* Lock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001386 spin_lock(&image->lock);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001387
Martyn Welch29848ac2010-02-18 15:13:05 +00001388 pci_addr_high = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001389 TSI148_LCSR_OFFSET_OTSAU);
Martyn Welch29848ac2010-02-18 15:13:05 +00001390 pci_addr_low = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001391 TSI148_LCSR_OFFSET_OTSAL);
1392
1393 reg_join(pci_addr_high, pci_addr_low, &pci_addr);
1394 reg_split(pci_addr + offset, &pci_addr_high, &pci_addr_low);
1395
1396 /* Configure registers */
Martyn Welch29848ac2010-02-18 15:13:05 +00001397 iowrite32be(mask, bridge->base + TSI148_LCSR_RMWEN);
1398 iowrite32be(compare, bridge->base + TSI148_LCSR_RMWC);
1399 iowrite32be(swap, bridge->base + TSI148_LCSR_RMWS);
1400 iowrite32be(pci_addr_high, bridge->base + TSI148_LCSR_RMWAU);
1401 iowrite32be(pci_addr_low, bridge->base + TSI148_LCSR_RMWAL);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001402
1403 /* Enable RMW */
Martyn Welch29848ac2010-02-18 15:13:05 +00001404 tmp = ioread32be(bridge->base + TSI148_LCSR_VMCTRL);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001405 tmp |= TSI148_LCSR_VMCTRL_RMWEN;
Martyn Welch29848ac2010-02-18 15:13:05 +00001406 iowrite32be(tmp, bridge->base + TSI148_LCSR_VMCTRL);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001407
1408 /* Kick process off with a read to the required address. */
1409 result = ioread32be(image->kern_base + offset);
1410
1411 /* Disable RMW */
Martyn Welch29848ac2010-02-18 15:13:05 +00001412 tmp = ioread32be(bridge->base + TSI148_LCSR_VMCTRL);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001413 tmp &= ~TSI148_LCSR_VMCTRL_RMWEN;
Martyn Welch29848ac2010-02-18 15:13:05 +00001414 iowrite32be(tmp, bridge->base + TSI148_LCSR_VMCTRL);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001415
Emilio G. Cota886953e2010-11-12 11:14:07 +00001416 spin_unlock(&image->lock);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001417
Emilio G. Cota886953e2010-11-12 11:14:07 +00001418 mutex_unlock(&bridge->vme_rmw);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001419
1420 return result;
1421}
1422
Martyn Welchac1a4f22012-03-22 13:27:30 +00001423static int tsi148_dma_set_vme_src_attributes(struct device *dev, __be32 *attr,
Martyn Welch6af04b02011-12-01 17:06:29 +00001424 u32 aspace, u32 cycle, u32 dwidth)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001425{
Martyn Welchac1a4f22012-03-22 13:27:30 +00001426 u32 val;
1427
1428 val = be32_to_cpu(*attr);
1429
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001430 /* Setup 2eSST speeds */
1431 switch (cycle & (VME_2eSST160 | VME_2eSST267 | VME_2eSST320)) {
1432 case VME_2eSST160:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001433 val |= TSI148_LCSR_DSAT_2eSSTM_160;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001434 break;
1435 case VME_2eSST267:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001436 val |= TSI148_LCSR_DSAT_2eSSTM_267;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001437 break;
1438 case VME_2eSST320:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001439 val |= TSI148_LCSR_DSAT_2eSSTM_320;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001440 break;
1441 }
1442
1443 /* Setup cycle types */
Martyn Welch79463282010-03-22 14:58:57 +00001444 if (cycle & VME_SCT)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001445 val |= TSI148_LCSR_DSAT_TM_SCT;
Martyn Welch79463282010-03-22 14:58:57 +00001446
1447 if (cycle & VME_BLT)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001448 val |= TSI148_LCSR_DSAT_TM_BLT;
Martyn Welch79463282010-03-22 14:58:57 +00001449
1450 if (cycle & VME_MBLT)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001451 val |= TSI148_LCSR_DSAT_TM_MBLT;
Martyn Welch79463282010-03-22 14:58:57 +00001452
1453 if (cycle & VME_2eVME)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001454 val |= TSI148_LCSR_DSAT_TM_2eVME;
Martyn Welch79463282010-03-22 14:58:57 +00001455
1456 if (cycle & VME_2eSST)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001457 val |= TSI148_LCSR_DSAT_TM_2eSST;
Martyn Welch79463282010-03-22 14:58:57 +00001458
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001459 if (cycle & VME_2eSSTB) {
Martyn Welch48d93562010-03-22 14:58:50 +00001460 dev_err(dev, "Currently not setting Broadcast Select "
1461 "Registers\n");
Martyn Welchac1a4f22012-03-22 13:27:30 +00001462 val |= TSI148_LCSR_DSAT_TM_2eSSTB;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001463 }
1464
1465 /* Setup data width */
1466 switch (dwidth) {
1467 case VME_D16:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001468 val |= TSI148_LCSR_DSAT_DBW_16;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001469 break;
1470 case VME_D32:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001471 val |= TSI148_LCSR_DSAT_DBW_32;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001472 break;
1473 default:
Martyn Welch48d93562010-03-22 14:58:50 +00001474 dev_err(dev, "Invalid data width\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001475 return -EINVAL;
1476 }
1477
1478 /* Setup address space */
1479 switch (aspace) {
1480 case VME_A16:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001481 val |= TSI148_LCSR_DSAT_AMODE_A16;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001482 break;
1483 case VME_A24:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001484 val |= TSI148_LCSR_DSAT_AMODE_A24;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001485 break;
1486 case VME_A32:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001487 val |= TSI148_LCSR_DSAT_AMODE_A32;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001488 break;
1489 case VME_A64:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001490 val |= TSI148_LCSR_DSAT_AMODE_A64;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001491 break;
1492 case VME_CRCSR:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001493 val |= TSI148_LCSR_DSAT_AMODE_CRCSR;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001494 break;
1495 case VME_USER1:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001496 val |= TSI148_LCSR_DSAT_AMODE_USER1;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001497 break;
1498 case VME_USER2:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001499 val |= TSI148_LCSR_DSAT_AMODE_USER2;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001500 break;
1501 case VME_USER3:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001502 val |= TSI148_LCSR_DSAT_AMODE_USER3;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001503 break;
1504 case VME_USER4:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001505 val |= TSI148_LCSR_DSAT_AMODE_USER4;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001506 break;
1507 default:
Martyn Welch48d93562010-03-22 14:58:50 +00001508 dev_err(dev, "Invalid address space\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001509 return -EINVAL;
1510 break;
1511 }
1512
1513 if (cycle & VME_SUPER)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001514 val |= TSI148_LCSR_DSAT_SUP;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001515 if (cycle & VME_PROG)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001516 val |= TSI148_LCSR_DSAT_PGM;
1517
1518 *attr = cpu_to_be32(val);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001519
1520 return 0;
1521}
1522
Martyn Welchac1a4f22012-03-22 13:27:30 +00001523static int tsi148_dma_set_vme_dest_attributes(struct device *dev, __be32 *attr,
Martyn Welch6af04b02011-12-01 17:06:29 +00001524 u32 aspace, u32 cycle, u32 dwidth)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001525{
Martyn Welchac1a4f22012-03-22 13:27:30 +00001526 u32 val;
1527
1528 val = be32_to_cpu(*attr);
1529
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001530 /* Setup 2eSST speeds */
1531 switch (cycle & (VME_2eSST160 | VME_2eSST267 | VME_2eSST320)) {
1532 case VME_2eSST160:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001533 val |= TSI148_LCSR_DDAT_2eSSTM_160;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001534 break;
1535 case VME_2eSST267:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001536 val |= TSI148_LCSR_DDAT_2eSSTM_267;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001537 break;
1538 case VME_2eSST320:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001539 val |= TSI148_LCSR_DDAT_2eSSTM_320;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001540 break;
1541 }
1542
1543 /* Setup cycle types */
Martyn Welch79463282010-03-22 14:58:57 +00001544 if (cycle & VME_SCT)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001545 val |= TSI148_LCSR_DDAT_TM_SCT;
Martyn Welch79463282010-03-22 14:58:57 +00001546
1547 if (cycle & VME_BLT)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001548 val |= TSI148_LCSR_DDAT_TM_BLT;
Martyn Welch79463282010-03-22 14:58:57 +00001549
1550 if (cycle & VME_MBLT)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001551 val |= TSI148_LCSR_DDAT_TM_MBLT;
Martyn Welch79463282010-03-22 14:58:57 +00001552
1553 if (cycle & VME_2eVME)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001554 val |= TSI148_LCSR_DDAT_TM_2eVME;
Martyn Welch79463282010-03-22 14:58:57 +00001555
1556 if (cycle & VME_2eSST)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001557 val |= TSI148_LCSR_DDAT_TM_2eSST;
Martyn Welch79463282010-03-22 14:58:57 +00001558
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001559 if (cycle & VME_2eSSTB) {
Martyn Welch48d93562010-03-22 14:58:50 +00001560 dev_err(dev, "Currently not setting Broadcast Select "
1561 "Registers\n");
Martyn Welchac1a4f22012-03-22 13:27:30 +00001562 val |= TSI148_LCSR_DDAT_TM_2eSSTB;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001563 }
1564
1565 /* Setup data width */
1566 switch (dwidth) {
1567 case VME_D16:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001568 val |= TSI148_LCSR_DDAT_DBW_16;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001569 break;
1570 case VME_D32:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001571 val |= TSI148_LCSR_DDAT_DBW_32;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001572 break;
1573 default:
Martyn Welch48d93562010-03-22 14:58:50 +00001574 dev_err(dev, "Invalid data width\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001575 return -EINVAL;
1576 }
1577
1578 /* Setup address space */
1579 switch (aspace) {
1580 case VME_A16:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001581 val |= TSI148_LCSR_DDAT_AMODE_A16;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001582 break;
1583 case VME_A24:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001584 val |= TSI148_LCSR_DDAT_AMODE_A24;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001585 break;
1586 case VME_A32:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001587 val |= TSI148_LCSR_DDAT_AMODE_A32;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001588 break;
1589 case VME_A64:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001590 val |= TSI148_LCSR_DDAT_AMODE_A64;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001591 break;
1592 case VME_CRCSR:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001593 val |= TSI148_LCSR_DDAT_AMODE_CRCSR;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001594 break;
1595 case VME_USER1:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001596 val |= TSI148_LCSR_DDAT_AMODE_USER1;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001597 break;
1598 case VME_USER2:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001599 val |= TSI148_LCSR_DDAT_AMODE_USER2;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001600 break;
1601 case VME_USER3:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001602 val |= TSI148_LCSR_DDAT_AMODE_USER3;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001603 break;
1604 case VME_USER4:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001605 val |= TSI148_LCSR_DDAT_AMODE_USER4;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001606 break;
1607 default:
Martyn Welch48d93562010-03-22 14:58:50 +00001608 dev_err(dev, "Invalid address space\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001609 return -EINVAL;
1610 break;
1611 }
1612
1613 if (cycle & VME_SUPER)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001614 val |= TSI148_LCSR_DDAT_SUP;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001615 if (cycle & VME_PROG)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001616 val |= TSI148_LCSR_DDAT_PGM;
1617
1618 *attr = cpu_to_be32(val);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001619
1620 return 0;
1621}
1622
1623/*
1624 * Add a link list descriptor to the list
Martyn Welchac1a4f22012-03-22 13:27:30 +00001625 *
1626 * Note: DMA engine expects the DMA descriptor to be big endian.
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001627 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00001628static int tsi148_dma_list_add(struct vme_dma_list *list,
1629 struct vme_dma_attr *src, struct vme_dma_attr *dest, size_t count)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001630{
1631 struct tsi148_dma_entry *entry, *prev;
Martyn Welchac1a4f22012-03-22 13:27:30 +00001632 u32 address_high, address_low, val;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001633 struct vme_dma_pattern *pattern_attr;
1634 struct vme_dma_pci *pci_attr;
1635 struct vme_dma_vme *vme_attr;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001636 int retval = 0;
Martyn Welch48d93562010-03-22 14:58:50 +00001637 struct vme_bridge *tsi148_bridge;
1638
1639 tsi148_bridge = list->parent->parent;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001640
Martyn Welchbb9ea892010-02-18 16:22:13 +00001641 /* Descriptor must be aligned on 64-bit boundaries */
Markus Elfring6d011dd2017-08-25 11:55:03 +02001642 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
Markus Elfringa75dc632017-08-25 12:00:17 +02001643 if (!entry) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001644 retval = -ENOMEM;
1645 goto err_mem;
1646 }
1647
1648 /* Test descriptor alignment */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001649 if ((unsigned long)&entry->descriptor & 0x7) {
Martyn Welch48d93562010-03-22 14:58:50 +00001650 dev_err(tsi148_bridge->parent, "Descriptor not aligned to 8 "
1651 "byte boundary as required: %p\n",
Emilio G. Cota886953e2010-11-12 11:14:07 +00001652 &entry->descriptor);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001653 retval = -EINVAL;
1654 goto err_align;
1655 }
1656
1657 /* Given we are going to fill out the structure, we probably don't
1658 * need to zero it, but better safe than sorry for now.
1659 */
Markus Elfring6d011dd2017-08-25 11:55:03 +02001660 memset(&entry->descriptor, 0, sizeof(entry->descriptor));
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001661
1662 /* Fill out source part */
1663 switch (src->type) {
1664 case VME_DMA_PATTERN:
Kulikov Vasiliyc4d82fb2010-06-29 14:16:20 +04001665 pattern_attr = src->private;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001666
Martyn Welchac1a4f22012-03-22 13:27:30 +00001667 entry->descriptor.dsal = cpu_to_be32(pattern_attr->pattern);
1668
1669 val = TSI148_LCSR_DSAT_TYP_PAT;
1670
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001671 /* Default behaviour is 32 bit pattern */
Martyn Welch79463282010-03-22 14:58:57 +00001672 if (pattern_attr->type & VME_DMA_PATTERN_BYTE)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001673 val |= TSI148_LCSR_DSAT_PSZ;
Martyn Welch79463282010-03-22 14:58:57 +00001674
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001675 /* It seems that the default behaviour is to increment */
Martyn Welch79463282010-03-22 14:58:57 +00001676 if ((pattern_attr->type & VME_DMA_PATTERN_INCREMENT) == 0)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001677 val |= TSI148_LCSR_DSAT_NIN;
1678 entry->descriptor.dsat = cpu_to_be32(val);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001679 break;
1680 case VME_DMA_PCI:
Kulikov Vasiliyc4d82fb2010-06-29 14:16:20 +04001681 pci_attr = src->private;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001682
1683 reg_split((unsigned long long)pci_attr->address, &address_high,
1684 &address_low);
Martyn Welchac1a4f22012-03-22 13:27:30 +00001685 entry->descriptor.dsau = cpu_to_be32(address_high);
1686 entry->descriptor.dsal = cpu_to_be32(address_low);
1687 entry->descriptor.dsat = cpu_to_be32(TSI148_LCSR_DSAT_TYP_PCI);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001688 break;
1689 case VME_DMA_VME:
Kulikov Vasiliyc4d82fb2010-06-29 14:16:20 +04001690 vme_attr = src->private;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001691
1692 reg_split((unsigned long long)vme_attr->address, &address_high,
1693 &address_low);
Martyn Welchac1a4f22012-03-22 13:27:30 +00001694 entry->descriptor.dsau = cpu_to_be32(address_high);
1695 entry->descriptor.dsal = cpu_to_be32(address_low);
1696 entry->descriptor.dsat = cpu_to_be32(TSI148_LCSR_DSAT_TYP_VME);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001697
1698 retval = tsi148_dma_set_vme_src_attributes(
Emilio G. Cota886953e2010-11-12 11:14:07 +00001699 tsi148_bridge->parent, &entry->descriptor.dsat,
Martyn Welch48d93562010-03-22 14:58:50 +00001700 vme_attr->aspace, vme_attr->cycle, vme_attr->dwidth);
Martyn Welch79463282010-03-22 14:58:57 +00001701 if (retval < 0)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001702 goto err_source;
1703 break;
1704 default:
Martyn Welch48d93562010-03-22 14:58:50 +00001705 dev_err(tsi148_bridge->parent, "Invalid source type\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001706 retval = -EINVAL;
1707 goto err_source;
1708 break;
1709 }
1710
1711 /* Assume last link - this will be over-written by adding another */
Martyn Welchac1a4f22012-03-22 13:27:30 +00001712 entry->descriptor.dnlau = cpu_to_be32(0);
1713 entry->descriptor.dnlal = cpu_to_be32(TSI148_LCSR_DNLAL_LLA);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001714
1715 /* Fill out destination part */
1716 switch (dest->type) {
1717 case VME_DMA_PCI:
Kulikov Vasiliyc4d82fb2010-06-29 14:16:20 +04001718 pci_attr = dest->private;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001719
1720 reg_split((unsigned long long)pci_attr->address, &address_high,
1721 &address_low);
Martyn Welchac1a4f22012-03-22 13:27:30 +00001722 entry->descriptor.ddau = cpu_to_be32(address_high);
1723 entry->descriptor.ddal = cpu_to_be32(address_low);
1724 entry->descriptor.ddat = cpu_to_be32(TSI148_LCSR_DDAT_TYP_PCI);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001725 break;
1726 case VME_DMA_VME:
Kulikov Vasiliyc4d82fb2010-06-29 14:16:20 +04001727 vme_attr = dest->private;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001728
1729 reg_split((unsigned long long)vme_attr->address, &address_high,
1730 &address_low);
Martyn Welchac1a4f22012-03-22 13:27:30 +00001731 entry->descriptor.ddau = cpu_to_be32(address_high);
1732 entry->descriptor.ddal = cpu_to_be32(address_low);
1733 entry->descriptor.ddat = cpu_to_be32(TSI148_LCSR_DDAT_TYP_VME);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001734
1735 retval = tsi148_dma_set_vme_dest_attributes(
Emilio G. Cota886953e2010-11-12 11:14:07 +00001736 tsi148_bridge->parent, &entry->descriptor.ddat,
Martyn Welch48d93562010-03-22 14:58:50 +00001737 vme_attr->aspace, vme_attr->cycle, vme_attr->dwidth);
Martyn Welch79463282010-03-22 14:58:57 +00001738 if (retval < 0)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001739 goto err_dest;
1740 break;
1741 default:
Martyn Welch48d93562010-03-22 14:58:50 +00001742 dev_err(tsi148_bridge->parent, "Invalid destination type\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001743 retval = -EINVAL;
1744 goto err_dest;
1745 break;
1746 }
1747
1748 /* Fill out count */
Martyn Welchac1a4f22012-03-22 13:27:30 +00001749 entry->descriptor.dcnt = cpu_to_be32((u32)count);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001750
1751 /* Add to list */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001752 list_add_tail(&entry->list, &list->entries);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001753
Dmitry Kalinkinb2383c92015-05-28 15:07:00 +03001754 entry->dma_handle = dma_map_single(tsi148_bridge->parent,
Markus Elfring6d011dd2017-08-25 11:55:03 +02001755 &entry->descriptor,
1756 sizeof(entry->descriptor),
1757 DMA_TO_DEVICE);
Dmitry Kalinkinb2383c92015-05-28 15:07:00 +03001758 if (dma_mapping_error(tsi148_bridge->parent, entry->dma_handle)) {
1759 dev_err(tsi148_bridge->parent, "DMA mapping error\n");
1760 retval = -EINVAL;
1761 goto err_dma;
1762 }
1763
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001764 /* Fill out previous descriptors "Next Address" */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001765 if (entry->list.prev != &list->entries) {
Martyn Welchac1a4f22012-03-22 13:27:30 +00001766 reg_split((unsigned long long)entry->dma_handle, &address_high,
1767 &address_low);
Dmitry Kalinkinb2383c92015-05-28 15:07:00 +03001768 prev = list_entry(entry->list.prev, struct tsi148_dma_entry,
1769 list);
Dmitry Kalinkinf656eaee2015-05-28 15:06:59 +03001770 prev->descriptor.dnlau = cpu_to_be32(address_high);
1771 prev->descriptor.dnlal = cpu_to_be32(address_low);
Martyn Welchac1a4f22012-03-22 13:27:30 +00001772
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001773 }
1774
1775 return 0;
1776
Dmitry Kalinkinb2383c92015-05-28 15:07:00 +03001777err_dma:
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001778err_dest:
1779err_source:
1780err_align:
1781 kfree(entry);
1782err_mem:
1783 return retval;
1784}
1785
1786/*
1787 * Check to see if the provided DMA channel is busy.
1788 */
Martyn Welch29848ac2010-02-18 15:13:05 +00001789static int tsi148_dma_busy(struct vme_bridge *tsi148_bridge, int channel)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001790{
1791 u32 tmp;
Martyn Welch29848ac2010-02-18 15:13:05 +00001792 struct tsi148_driver *bridge;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001793
Martyn Welch29848ac2010-02-18 15:13:05 +00001794 bridge = tsi148_bridge->driver_priv;
1795
1796 tmp = ioread32be(bridge->base + TSI148_LCSR_DMA[channel] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001797 TSI148_LCSR_OFFSET_DSTA);
1798
1799 if (tmp & TSI148_LCSR_DSTA_BSY)
1800 return 0;
1801 else
1802 return 1;
1803
1804}
1805
1806/*
1807 * Execute a previously generated link list
1808 *
1809 * XXX Need to provide control register configuration.
1810 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00001811static int tsi148_dma_list_exec(struct vme_dma_list *list)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001812{
1813 struct vme_dma_resource *ctrlr;
Dmitry Kalinkin75c66b62015-05-28 15:07:01 +03001814 int channel, retval;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001815 struct tsi148_dma_entry *entry;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001816 u32 bus_addr_high, bus_addr_low;
1817 u32 val, dctlreg = 0;
Martyn Welch48d93562010-03-22 14:58:50 +00001818 struct vme_bridge *tsi148_bridge;
Martyn Welch29848ac2010-02-18 15:13:05 +00001819 struct tsi148_driver *bridge;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001820
1821 ctrlr = list->parent;
1822
Martyn Welch48d93562010-03-22 14:58:50 +00001823 tsi148_bridge = ctrlr->parent;
1824
1825 bridge = tsi148_bridge->driver_priv;
Martyn Welch29848ac2010-02-18 15:13:05 +00001826
Emilio G. Cota886953e2010-11-12 11:14:07 +00001827 mutex_lock(&ctrlr->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001828
1829 channel = ctrlr->number;
1830
Emilio G. Cota886953e2010-11-12 11:14:07 +00001831 if (!list_empty(&ctrlr->running)) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001832 /*
1833 * XXX We have an active DMA transfer and currently haven't
1834 * sorted out the mechanism for "pending" DMA transfers.
1835 * Return busy.
1836 */
1837 /* Need to add to pending here */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001838 mutex_unlock(&ctrlr->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001839 return -EBUSY;
1840 } else {
Emilio G. Cota886953e2010-11-12 11:14:07 +00001841 list_add(&list->list, &ctrlr->running);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001842 }
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001843
1844 /* Get first bus address and write into registers */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001845 entry = list_first_entry(&list->entries, struct tsi148_dma_entry,
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001846 list);
1847
Emilio G. Cota886953e2010-11-12 11:14:07 +00001848 mutex_unlock(&ctrlr->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001849
Martyn Welch3abc48a2012-03-22 13:27:29 +00001850 reg_split(entry->dma_handle, &bus_addr_high, &bus_addr_low);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001851
Martyn Welch29848ac2010-02-18 15:13:05 +00001852 iowrite32be(bus_addr_high, bridge->base +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001853 TSI148_LCSR_DMA[channel] + TSI148_LCSR_OFFSET_DNLAU);
Martyn Welch29848ac2010-02-18 15:13:05 +00001854 iowrite32be(bus_addr_low, bridge->base +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001855 TSI148_LCSR_DMA[channel] + TSI148_LCSR_OFFSET_DNLAL);
1856
Martyn Welchac1a4f22012-03-22 13:27:30 +00001857 dctlreg = ioread32be(bridge->base + TSI148_LCSR_DMA[channel] +
1858 TSI148_LCSR_OFFSET_DCTL);
1859
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001860 /* Start the operation */
Martyn Welch29848ac2010-02-18 15:13:05 +00001861 iowrite32be(dctlreg | TSI148_LCSR_DCTL_DGO, bridge->base +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001862 TSI148_LCSR_DMA[channel] + TSI148_LCSR_OFFSET_DCTL);
1863
Dmitry Kalinkin75c66b62015-05-28 15:07:01 +03001864 retval = wait_event_interruptible(bridge->dma_queue[channel],
Martyn Welch29848ac2010-02-18 15:13:05 +00001865 tsi148_dma_busy(ctrlr->parent, channel));
Martyn Welchac1a4f22012-03-22 13:27:30 +00001866
Dmitry Kalinkin75c66b62015-05-28 15:07:01 +03001867 if (retval) {
1868 iowrite32be(dctlreg | TSI148_LCSR_DCTL_ABT, bridge->base +
1869 TSI148_LCSR_DMA[channel] + TSI148_LCSR_OFFSET_DCTL);
1870 /* Wait for the operation to abort */
1871 wait_event(bridge->dma_queue[channel],
1872 tsi148_dma_busy(ctrlr->parent, channel));
1873 retval = -EINTR;
1874 goto exit;
1875 }
1876
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001877 /*
1878 * Read status register, this register is valid until we kick off a
1879 * new transfer.
1880 */
Martyn Welch29848ac2010-02-18 15:13:05 +00001881 val = ioread32be(bridge->base + TSI148_LCSR_DMA[channel] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001882 TSI148_LCSR_OFFSET_DSTA);
1883
1884 if (val & TSI148_LCSR_DSTA_VBE) {
Martyn Welch48d93562010-03-22 14:58:50 +00001885 dev_err(tsi148_bridge->parent, "DMA Error. DSTA=%08X\n", val);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001886 retval = -EIO;
1887 }
1888
Dmitry Kalinkin75c66b62015-05-28 15:07:01 +03001889exit:
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001890 /* Remove list from running list */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001891 mutex_lock(&ctrlr->mtx);
1892 list_del(&list->list);
1893 mutex_unlock(&ctrlr->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001894
1895 return retval;
1896}
1897
1898/*
1899 * Clean up a previously generated link list
1900 *
1901 * We have a separate function, don't assume that the chain can't be reused.
1902 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00001903static int tsi148_dma_list_empty(struct vme_dma_list *list)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001904{
1905 struct list_head *pos, *temp;
Martyn Welch79463282010-03-22 14:58:57 +00001906 struct tsi148_dma_entry *entry;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001907
Martyn Welch3abc48a2012-03-22 13:27:29 +00001908 struct vme_bridge *tsi148_bridge = list->parent->parent;
1909
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001910 /* detach and free each entry */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001911 list_for_each_safe(pos, temp, &list->entries) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001912 list_del(pos);
1913 entry = list_entry(pos, struct tsi148_dma_entry, list);
Martyn Welch3abc48a2012-03-22 13:27:29 +00001914
1915 dma_unmap_single(tsi148_bridge->parent, entry->dma_handle,
1916 sizeof(struct tsi148_dma_descriptor), DMA_TO_DEVICE);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001917 kfree(entry);
1918 }
1919
Martyn Welch79463282010-03-22 14:58:57 +00001920 return 0;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001921}
1922
1923/*
1924 * All 4 location monitors reside at the same base - this is therefore a
1925 * system wide configuration.
1926 *
1927 * This does not enable the LM monitor - that should be done when the first
1928 * callback is attached and disabled when the last callback is removed.
1929 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00001930static int tsi148_lm_set(struct vme_lm_resource *lm, unsigned long long lm_base,
Martyn Welch6af04b02011-12-01 17:06:29 +00001931 u32 aspace, u32 cycle)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001932{
1933 u32 lm_base_high, lm_base_low, lm_ctl = 0;
1934 int i;
Martyn Welch48d93562010-03-22 14:58:50 +00001935 struct vme_bridge *tsi148_bridge;
Martyn Welch29848ac2010-02-18 15:13:05 +00001936 struct tsi148_driver *bridge;
1937
Martyn Welch48d93562010-03-22 14:58:50 +00001938 tsi148_bridge = lm->parent;
1939
1940 bridge = tsi148_bridge->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001941
Emilio G. Cota886953e2010-11-12 11:14:07 +00001942 mutex_lock(&lm->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001943
1944 /* If we already have a callback attached, we can't move it! */
Martyn Welch42fb5032009-08-11 17:44:56 +01001945 for (i = 0; i < lm->monitors; i++) {
Markus Elfringa75dc632017-08-25 12:00:17 +02001946 if (bridge->lm_callback[i]) {
Emilio G. Cota886953e2010-11-12 11:14:07 +00001947 mutex_unlock(&lm->mtx);
Martyn Welch48d93562010-03-22 14:58:50 +00001948 dev_err(tsi148_bridge->parent, "Location monitor "
1949 "callback attached, can't reset\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001950 return -EBUSY;
1951 }
1952 }
1953
1954 switch (aspace) {
1955 case VME_A16:
1956 lm_ctl |= TSI148_LCSR_LMAT_AS_A16;
1957 break;
1958 case VME_A24:
1959 lm_ctl |= TSI148_LCSR_LMAT_AS_A24;
1960 break;
1961 case VME_A32:
1962 lm_ctl |= TSI148_LCSR_LMAT_AS_A32;
1963 break;
1964 case VME_A64:
1965 lm_ctl |= TSI148_LCSR_LMAT_AS_A64;
1966 break;
1967 default:
Emilio G. Cota886953e2010-11-12 11:14:07 +00001968 mutex_unlock(&lm->mtx);
Martyn Welch48d93562010-03-22 14:58:50 +00001969 dev_err(tsi148_bridge->parent, "Invalid address space\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001970 return -EINVAL;
1971 break;
1972 }
1973
1974 if (cycle & VME_SUPER)
1975 lm_ctl |= TSI148_LCSR_LMAT_SUPR ;
1976 if (cycle & VME_USER)
1977 lm_ctl |= TSI148_LCSR_LMAT_NPRIV;
1978 if (cycle & VME_PROG)
1979 lm_ctl |= TSI148_LCSR_LMAT_PGM;
1980 if (cycle & VME_DATA)
1981 lm_ctl |= TSI148_LCSR_LMAT_DATA;
1982
1983 reg_split(lm_base, &lm_base_high, &lm_base_low);
1984
Martyn Welch29848ac2010-02-18 15:13:05 +00001985 iowrite32be(lm_base_high, bridge->base + TSI148_LCSR_LMBAU);
1986 iowrite32be(lm_base_low, bridge->base + TSI148_LCSR_LMBAL);
1987 iowrite32be(lm_ctl, bridge->base + TSI148_LCSR_LMAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001988
Emilio G. Cota886953e2010-11-12 11:14:07 +00001989 mutex_unlock(&lm->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001990
1991 return 0;
1992}
1993
1994/* Get configuration of the callback monitor and return whether it is enabled
1995 * or disabled.
1996 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00001997static int tsi148_lm_get(struct vme_lm_resource *lm,
Martyn Welch6af04b02011-12-01 17:06:29 +00001998 unsigned long long *lm_base, u32 *aspace, u32 *cycle)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001999{
2000 u32 lm_base_high, lm_base_low, lm_ctl, enabled = 0;
Martyn Welch29848ac2010-02-18 15:13:05 +00002001 struct tsi148_driver *bridge;
2002
2003 bridge = lm->parent->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002004
Emilio G. Cota886953e2010-11-12 11:14:07 +00002005 mutex_lock(&lm->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002006
Martyn Welch29848ac2010-02-18 15:13:05 +00002007 lm_base_high = ioread32be(bridge->base + TSI148_LCSR_LMBAU);
2008 lm_base_low = ioread32be(bridge->base + TSI148_LCSR_LMBAL);
2009 lm_ctl = ioread32be(bridge->base + TSI148_LCSR_LMAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002010
2011 reg_join(lm_base_high, lm_base_low, lm_base);
2012
2013 if (lm_ctl & TSI148_LCSR_LMAT_EN)
2014 enabled = 1;
2015
Martyn Welch79463282010-03-22 14:58:57 +00002016 if ((lm_ctl & TSI148_LCSR_LMAT_AS_M) == TSI148_LCSR_LMAT_AS_A16)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002017 *aspace |= VME_A16;
Martyn Welch79463282010-03-22 14:58:57 +00002018
2019 if ((lm_ctl & TSI148_LCSR_LMAT_AS_M) == TSI148_LCSR_LMAT_AS_A24)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002020 *aspace |= VME_A24;
Martyn Welch79463282010-03-22 14:58:57 +00002021
2022 if ((lm_ctl & TSI148_LCSR_LMAT_AS_M) == TSI148_LCSR_LMAT_AS_A32)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002023 *aspace |= VME_A32;
Martyn Welch79463282010-03-22 14:58:57 +00002024
2025 if ((lm_ctl & TSI148_LCSR_LMAT_AS_M) == TSI148_LCSR_LMAT_AS_A64)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002026 *aspace |= VME_A64;
Martyn Welch79463282010-03-22 14:58:57 +00002027
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002028
2029 if (lm_ctl & TSI148_LCSR_LMAT_SUPR)
2030 *cycle |= VME_SUPER;
2031 if (lm_ctl & TSI148_LCSR_LMAT_NPRIV)
2032 *cycle |= VME_USER;
2033 if (lm_ctl & TSI148_LCSR_LMAT_PGM)
2034 *cycle |= VME_PROG;
2035 if (lm_ctl & TSI148_LCSR_LMAT_DATA)
2036 *cycle |= VME_DATA;
2037
Emilio G. Cota886953e2010-11-12 11:14:07 +00002038 mutex_unlock(&lm->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002039
2040 return enabled;
2041}
2042
2043/*
2044 * Attach a callback to a specific location monitor.
2045 *
2046 * Callback will be passed the monitor triggered.
2047 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00002048static int tsi148_lm_attach(struct vme_lm_resource *lm, int monitor,
Aaron Sierrafa54b322016-04-29 16:41:02 -05002049 void (*callback)(void *), void *data)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002050{
2051 u32 lm_ctl, tmp;
Martyn Welch48d93562010-03-22 14:58:50 +00002052 struct vme_bridge *tsi148_bridge;
Martyn Welch29848ac2010-02-18 15:13:05 +00002053 struct tsi148_driver *bridge;
2054
Martyn Welch48d93562010-03-22 14:58:50 +00002055 tsi148_bridge = lm->parent;
2056
2057 bridge = tsi148_bridge->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002058
Emilio G. Cota886953e2010-11-12 11:14:07 +00002059 mutex_lock(&lm->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002060
2061 /* Ensure that the location monitor is configured - need PGM or DATA */
Martyn Welch29848ac2010-02-18 15:13:05 +00002062 lm_ctl = ioread32be(bridge->base + TSI148_LCSR_LMAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002063 if ((lm_ctl & (TSI148_LCSR_LMAT_PGM | TSI148_LCSR_LMAT_DATA)) == 0) {
Emilio G. Cota886953e2010-11-12 11:14:07 +00002064 mutex_unlock(&lm->mtx);
Martyn Welch48d93562010-03-22 14:58:50 +00002065 dev_err(tsi148_bridge->parent, "Location monitor not properly "
2066 "configured\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002067 return -EINVAL;
2068 }
2069
2070 /* Check that a callback isn't already attached */
Markus Elfringa75dc632017-08-25 12:00:17 +02002071 if (bridge->lm_callback[monitor]) {
Emilio G. Cota886953e2010-11-12 11:14:07 +00002072 mutex_unlock(&lm->mtx);
Martyn Welch48d93562010-03-22 14:58:50 +00002073 dev_err(tsi148_bridge->parent, "Existing callback attached\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002074 return -EBUSY;
2075 }
2076
2077 /* Attach callback */
Martyn Welch29848ac2010-02-18 15:13:05 +00002078 bridge->lm_callback[monitor] = callback;
Aaron Sierrafa54b322016-04-29 16:41:02 -05002079 bridge->lm_data[monitor] = data;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002080
2081 /* Enable Location Monitor interrupt */
Martyn Welch29848ac2010-02-18 15:13:05 +00002082 tmp = ioread32be(bridge->base + TSI148_LCSR_INTEN);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002083 tmp |= TSI148_LCSR_INTEN_LMEN[monitor];
Martyn Welch29848ac2010-02-18 15:13:05 +00002084 iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEN);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002085
Martyn Welch29848ac2010-02-18 15:13:05 +00002086 tmp = ioread32be(bridge->base + TSI148_LCSR_INTEO);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002087 tmp |= TSI148_LCSR_INTEO_LMEO[monitor];
Martyn Welch29848ac2010-02-18 15:13:05 +00002088 iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEO);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002089
2090 /* Ensure that global Location Monitor Enable set */
2091 if ((lm_ctl & TSI148_LCSR_LMAT_EN) == 0) {
2092 lm_ctl |= TSI148_LCSR_LMAT_EN;
Martyn Welch29848ac2010-02-18 15:13:05 +00002093 iowrite32be(lm_ctl, bridge->base + TSI148_LCSR_LMAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002094 }
2095
Emilio G. Cota886953e2010-11-12 11:14:07 +00002096 mutex_unlock(&lm->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002097
2098 return 0;
2099}
2100
2101/*
2102 * Detach a callback function forn a specific location monitor.
2103 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00002104static int tsi148_lm_detach(struct vme_lm_resource *lm, int monitor)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002105{
2106 u32 lm_en, tmp;
Martyn Welch29848ac2010-02-18 15:13:05 +00002107 struct tsi148_driver *bridge;
2108
2109 bridge = lm->parent->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002110
Emilio G. Cota886953e2010-11-12 11:14:07 +00002111 mutex_lock(&lm->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002112
2113 /* Disable Location Monitor and ensure previous interrupts are clear */
Martyn Welch29848ac2010-02-18 15:13:05 +00002114 lm_en = ioread32be(bridge->base + TSI148_LCSR_INTEN);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002115 lm_en &= ~TSI148_LCSR_INTEN_LMEN[monitor];
Martyn Welch29848ac2010-02-18 15:13:05 +00002116 iowrite32be(lm_en, bridge->base + TSI148_LCSR_INTEN);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002117
Martyn Welch29848ac2010-02-18 15:13:05 +00002118 tmp = ioread32be(bridge->base + TSI148_LCSR_INTEO);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002119 tmp &= ~TSI148_LCSR_INTEO_LMEO[monitor];
Martyn Welch29848ac2010-02-18 15:13:05 +00002120 iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEO);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002121
2122 iowrite32be(TSI148_LCSR_INTC_LMC[monitor],
Martyn Welch29848ac2010-02-18 15:13:05 +00002123 bridge->base + TSI148_LCSR_INTC);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002124
2125 /* Detach callback */
Martyn Welch29848ac2010-02-18 15:13:05 +00002126 bridge->lm_callback[monitor] = NULL;
Aaron Sierrafa54b322016-04-29 16:41:02 -05002127 bridge->lm_data[monitor] = NULL;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002128
2129 /* If all location monitors disabled, disable global Location Monitor */
2130 if ((lm_en & (TSI148_LCSR_INTS_LM0S | TSI148_LCSR_INTS_LM1S |
2131 TSI148_LCSR_INTS_LM2S | TSI148_LCSR_INTS_LM3S)) == 0) {
Martyn Welch29848ac2010-02-18 15:13:05 +00002132 tmp = ioread32be(bridge->base + TSI148_LCSR_LMAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002133 tmp &= ~TSI148_LCSR_LMAT_EN;
Martyn Welch29848ac2010-02-18 15:13:05 +00002134 iowrite32be(tmp, bridge->base + TSI148_LCSR_LMAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002135 }
2136
Emilio G. Cota886953e2010-11-12 11:14:07 +00002137 mutex_unlock(&lm->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002138
2139 return 0;
2140}
2141
2142/*
2143 * Determine Geographical Addressing
2144 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00002145static int tsi148_slot_get(struct vme_bridge *tsi148_bridge)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002146{
Martyn Welch79463282010-03-22 14:58:57 +00002147 u32 slot = 0;
Martyn Welch29848ac2010-02-18 15:13:05 +00002148 struct tsi148_driver *bridge;
2149
2150 bridge = tsi148_bridge->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002151
Martyn Welch638f1992009-12-15 08:42:49 +00002152 if (!geoid) {
Martyn Welch29848ac2010-02-18 15:13:05 +00002153 slot = ioread32be(bridge->base + TSI148_LCSR_VSTAT);
Martyn Welch638f1992009-12-15 08:42:49 +00002154 slot = slot & TSI148_LCSR_VSTAT_GA_M;
2155 } else
2156 slot = geoid;
2157
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002158 return (int)slot;
2159}
2160
H Hartley Sweeten lin8a508ff2012-05-02 17:08:38 -07002161static void *tsi148_alloc_consistent(struct device *parent, size_t size,
Manohar Vanga7f58f022011-08-10 11:33:46 +02002162 dma_addr_t *dma)
2163{
2164 struct pci_dev *pdev;
2165
2166 /* Find pci_dev container of dev */
Aaron Sierra177581fa2014-04-03 14:48:27 -05002167 pdev = to_pci_dev(parent);
Manohar Vanga7f58f022011-08-10 11:33:46 +02002168
2169 return pci_alloc_consistent(pdev, size, dma);
2170}
2171
H Hartley Sweeten lin8a508ff2012-05-02 17:08:38 -07002172static void tsi148_free_consistent(struct device *parent, size_t size,
2173 void *vaddr, dma_addr_t dma)
Manohar Vanga7f58f022011-08-10 11:33:46 +02002174{
2175 struct pci_dev *pdev;
2176
2177 /* Find pci_dev container of dev */
Aaron Sierra177581fa2014-04-03 14:48:27 -05002178 pdev = to_pci_dev(parent);
Manohar Vanga7f58f022011-08-10 11:33:46 +02002179
2180 pci_free_consistent(pdev, size, vaddr, dma);
2181}
2182
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002183/*
2184 * Configure CR/CSR space
2185 *
2186 * Access to the CR/CSR can be configured at power-up. The location of the
2187 * CR/CSR registers in the CR/CSR address space is determined by the boards
2188 * Auto-ID or Geographic address. This function ensures that the window is
2189 * enabled at an offset consistent with the boards geopgraphic address.
2190 *
2191 * Each board has a 512kB window, with the highest 4kB being used for the
2192 * boards registers, this means there is a fix length 508kB window which must
2193 * be mapped onto PCI memory.
2194 */
Martyn Welch29848ac2010-02-18 15:13:05 +00002195static int tsi148_crcsr_init(struct vme_bridge *tsi148_bridge,
2196 struct pci_dev *pdev)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002197{
2198 u32 cbar, crat, vstat;
2199 u32 crcsr_bus_high, crcsr_bus_low;
2200 int retval;
Martyn Welch29848ac2010-02-18 15:13:05 +00002201 struct tsi148_driver *bridge;
2202
2203 bridge = tsi148_bridge->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002204
2205 /* Allocate mem for CR/CSR image */
Joe Perches88b26082014-08-08 14:24:53 -07002206 bridge->crcsr_kernel = pci_zalloc_consistent(pdev, VME_CRCSR_BUF_SIZE,
2207 &bridge->crcsr_bus);
Markus Elfringa75dc632017-08-25 12:00:17 +02002208 if (!bridge->crcsr_kernel) {
Martyn Welch48d93562010-03-22 14:58:50 +00002209 dev_err(tsi148_bridge->parent, "Failed to allocate memory for "
2210 "CR/CSR image\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002211 return -ENOMEM;
2212 }
2213
Martyn Welch29848ac2010-02-18 15:13:05 +00002214 reg_split(bridge->crcsr_bus, &crcsr_bus_high, &crcsr_bus_low);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002215
Martyn Welch29848ac2010-02-18 15:13:05 +00002216 iowrite32be(crcsr_bus_high, bridge->base + TSI148_LCSR_CROU);
2217 iowrite32be(crcsr_bus_low, bridge->base + TSI148_LCSR_CROL);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002218
2219 /* Ensure that the CR/CSR is configured at the correct offset */
Martyn Welch29848ac2010-02-18 15:13:05 +00002220 cbar = ioread32be(bridge->base + TSI148_CBAR);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002221 cbar = (cbar & TSI148_CRCSR_CBAR_M)>>3;
2222
Martyn Welch29848ac2010-02-18 15:13:05 +00002223 vstat = tsi148_slot_get(tsi148_bridge);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002224
2225 if (cbar != vstat) {
Martyn Welch638f1992009-12-15 08:42:49 +00002226 cbar = vstat;
Martyn Welch48d93562010-03-22 14:58:50 +00002227 dev_info(tsi148_bridge->parent, "Setting CR/CSR offset\n");
Martyn Welch29848ac2010-02-18 15:13:05 +00002228 iowrite32be(cbar<<3, bridge->base + TSI148_CBAR);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002229 }
Martyn Welch48d93562010-03-22 14:58:50 +00002230 dev_info(tsi148_bridge->parent, "CR/CSR Offset: %d\n", cbar);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002231
Martyn Welch29848ac2010-02-18 15:13:05 +00002232 crat = ioread32be(bridge->base + TSI148_LCSR_CRAT);
Martyn Welch29817952013-06-11 17:03:14 +01002233 if (crat & TSI148_LCSR_CRAT_EN)
2234 dev_info(tsi148_bridge->parent, "CR/CSR already enabled\n");
2235 else {
Martyn Welch48d93562010-03-22 14:58:50 +00002236 dev_info(tsi148_bridge->parent, "Enabling CR/CSR space\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002237 iowrite32be(crat | TSI148_LCSR_CRAT_EN,
Martyn Welch29848ac2010-02-18 15:13:05 +00002238 bridge->base + TSI148_LCSR_CRAT);
Martyn Welch29817952013-06-11 17:03:14 +01002239 }
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002240
2241 /* If we want flushed, error-checked writes, set up a window
2242 * over the CR/CSR registers. We read from here to safely flush
2243 * through VME writes.
2244 */
Martyn Welch79463282010-03-22 14:58:57 +00002245 if (err_chk) {
Martyn Welch29848ac2010-02-18 15:13:05 +00002246 retval = tsi148_master_set(bridge->flush_image, 1,
2247 (vstat * 0x80000), 0x80000, VME_CRCSR, VME_SCT,
2248 VME_D16);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002249 if (retval)
Martyn Welch48d93562010-03-22 14:58:50 +00002250 dev_err(tsi148_bridge->parent, "Configuring flush image"
2251 " failed\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002252 }
2253
2254 return 0;
2255
2256}
2257
Martyn Welch29848ac2010-02-18 15:13:05 +00002258static void tsi148_crcsr_exit(struct vme_bridge *tsi148_bridge,
2259 struct pci_dev *pdev)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002260{
2261 u32 crat;
Martyn Welch29848ac2010-02-18 15:13:05 +00002262 struct tsi148_driver *bridge;
2263
2264 bridge = tsi148_bridge->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002265
2266 /* Turn off CR/CSR space */
Martyn Welch29848ac2010-02-18 15:13:05 +00002267 crat = ioread32be(bridge->base + TSI148_LCSR_CRAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002268 iowrite32be(crat & ~TSI148_LCSR_CRAT_EN,
Martyn Welch29848ac2010-02-18 15:13:05 +00002269 bridge->base + TSI148_LCSR_CRAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002270
2271 /* Free image */
Martyn Welch29848ac2010-02-18 15:13:05 +00002272 iowrite32be(0, bridge->base + TSI148_LCSR_CROU);
2273 iowrite32be(0, bridge->base + TSI148_LCSR_CROL);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002274
Martyn Welch29848ac2010-02-18 15:13:05 +00002275 pci_free_consistent(pdev, VME_CRCSR_BUF_SIZE, bridge->crcsr_kernel,
2276 bridge->crcsr_bus);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002277}
2278
2279static int tsi148_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2280{
2281 int retval, i, master_num;
2282 u32 data;
Wei Yongjunb49c32b2012-08-21 12:19:01 +08002283 struct list_head *pos = NULL, *n;
Martyn Welch29848ac2010-02-18 15:13:05 +00002284 struct vme_bridge *tsi148_bridge;
2285 struct tsi148_driver *tsi148_device;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002286 struct vme_master_resource *master_image;
2287 struct vme_slave_resource *slave_image;
2288 struct vme_dma_resource *dma_ctrlr;
Martyn Welch42fb5032009-08-11 17:44:56 +01002289 struct vme_lm_resource *lm;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002290
2291 /* If we want to support more than one of each bridge, we need to
2292 * dynamically generate this so we get one per device
2293 */
Markus Elfring6d011dd2017-08-25 11:55:03 +02002294 tsi148_bridge = kzalloc(sizeof(*tsi148_bridge), GFP_KERNEL);
Markus Elfringa75dc632017-08-25 12:00:17 +02002295 if (!tsi148_bridge) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002296 retval = -ENOMEM;
2297 goto err_struct;
2298 }
Aaron Sierra326071b2016-04-24 15:11:38 -05002299 vme_init_bridge(tsi148_bridge);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002300
Markus Elfring6d011dd2017-08-25 11:55:03 +02002301 tsi148_device = kzalloc(sizeof(*tsi148_device), GFP_KERNEL);
Markus Elfringa75dc632017-08-25 12:00:17 +02002302 if (!tsi148_device) {
Martyn Welch29848ac2010-02-18 15:13:05 +00002303 retval = -ENOMEM;
2304 goto err_driver;
2305 }
2306
Martyn Welch29848ac2010-02-18 15:13:05 +00002307 tsi148_bridge->driver_priv = tsi148_device;
2308
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002309 /* Enable the device */
2310 retval = pci_enable_device(pdev);
2311 if (retval) {
2312 dev_err(&pdev->dev, "Unable to enable device\n");
2313 goto err_enable;
2314 }
2315
2316 /* Map Registers */
2317 retval = pci_request_regions(pdev, driver_name);
2318 if (retval) {
2319 dev_err(&pdev->dev, "Unable to reserve resources\n");
2320 goto err_resource;
2321 }
2322
2323 /* map registers in BAR 0 */
Martyn Welch29848ac2010-02-18 15:13:05 +00002324 tsi148_device->base = ioremap_nocache(pci_resource_start(pdev, 0),
2325 4096);
2326 if (!tsi148_device->base) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002327 dev_err(&pdev->dev, "Unable to remap CRG region\n");
2328 retval = -EIO;
2329 goto err_remap;
2330 }
2331
2332 /* Check to see if the mapping worked out */
Martyn Welch29848ac2010-02-18 15:13:05 +00002333 data = ioread32(tsi148_device->base + TSI148_PCFS_ID) & 0x0000FFFF;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002334 if (data != PCI_VENDOR_ID_TUNDRA) {
2335 dev_err(&pdev->dev, "CRG region check failed\n");
2336 retval = -EIO;
2337 goto err_test;
2338 }
2339
2340 /* Initialize wait queues & mutual exclusion flags */
Emilio G. Cota886953e2010-11-12 11:14:07 +00002341 init_waitqueue_head(&tsi148_device->dma_queue[0]);
2342 init_waitqueue_head(&tsi148_device->dma_queue[1]);
2343 init_waitqueue_head(&tsi148_device->iack_queue);
2344 mutex_init(&tsi148_device->vme_int);
2345 mutex_init(&tsi148_device->vme_rmw);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002346
Emilio G. Cota886953e2010-11-12 11:14:07 +00002347 tsi148_bridge->parent = &pdev->dev;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002348 strcpy(tsi148_bridge->name, driver_name);
2349
2350 /* Setup IRQ */
2351 retval = tsi148_irq_init(tsi148_bridge);
2352 if (retval != 0) {
2353 dev_err(&pdev->dev, "Chip Initialization failed.\n");
2354 goto err_irq;
2355 }
2356
2357 /* If we are going to flush writes, we need to read from the VME bus.
2358 * We need to do this safely, thus we read the devices own CR/CSR
2359 * register. To do this we must set up a window in CR/CSR space and
2360 * hence have one less master window resource available.
2361 */
2362 master_num = TSI148_MAX_MASTER;
Martyn Welch79463282010-03-22 14:58:57 +00002363 if (err_chk) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002364 master_num--;
Martyn Welch29848ac2010-02-18 15:13:05 +00002365
Julia Lawall32414872010-05-11 20:26:57 +02002366 tsi148_device->flush_image =
Markus Elfring6d011dd2017-08-25 11:55:03 +02002367 kmalloc(sizeof(*tsi148_device->flush_image),
2368 GFP_KERNEL);
Markus Elfringa75dc632017-08-25 12:00:17 +02002369 if (!tsi148_device->flush_image) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002370 retval = -ENOMEM;
2371 goto err_master;
2372 }
Martyn Welch29848ac2010-02-18 15:13:05 +00002373 tsi148_device->flush_image->parent = tsi148_bridge;
Emilio G. Cota886953e2010-11-12 11:14:07 +00002374 spin_lock_init(&tsi148_device->flush_image->lock);
Martyn Welch29848ac2010-02-18 15:13:05 +00002375 tsi148_device->flush_image->locked = 1;
2376 tsi148_device->flush_image->number = master_num;
Emilio G. Cota886953e2010-11-12 11:14:07 +00002377 memset(&tsi148_device->flush_image->bus_resource, 0,
Markus Elfring6d011dd2017-08-25 11:55:03 +02002378 sizeof(tsi148_device->flush_image->bus_resource));
Martyn Welch29848ac2010-02-18 15:13:05 +00002379 tsi148_device->flush_image->kern_base = NULL;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002380 }
2381
2382 /* Add master windows to list */
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002383 for (i = 0; i < master_num; i++) {
Markus Elfring6d011dd2017-08-25 11:55:03 +02002384 master_image = kmalloc(sizeof(*master_image), GFP_KERNEL);
Markus Elfringa75dc632017-08-25 12:00:17 +02002385 if (!master_image) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002386 retval = -ENOMEM;
2387 goto err_master;
2388 }
2389 master_image->parent = tsi148_bridge;
Emilio G. Cota886953e2010-11-12 11:14:07 +00002390 spin_lock_init(&master_image->lock);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002391 master_image->locked = 0;
2392 master_image->number = i;
2393 master_image->address_attr = VME_A16 | VME_A24 | VME_A32 |
Martyn Welch08e03c22015-02-26 18:53:11 +03002394 VME_A64 | VME_CRCSR | VME_USER1 | VME_USER2 |
2395 VME_USER3 | VME_USER4;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002396 master_image->cycle_attr = VME_SCT | VME_BLT | VME_MBLT |
2397 VME_2eVME | VME_2eSST | VME_2eSSTB | VME_2eSST160 |
2398 VME_2eSST267 | VME_2eSST320 | VME_SUPER | VME_USER |
2399 VME_PROG | VME_DATA;
2400 master_image->width_attr = VME_D16 | VME_D32;
Emilio G. Cota886953e2010-11-12 11:14:07 +00002401 memset(&master_image->bus_resource, 0,
Markus Elfring6d011dd2017-08-25 11:55:03 +02002402 sizeof(master_image->bus_resource));
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002403 master_image->kern_base = NULL;
Emilio G. Cota886953e2010-11-12 11:14:07 +00002404 list_add_tail(&master_image->list,
2405 &tsi148_bridge->master_resources);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002406 }
2407
2408 /* Add slave windows to list */
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002409 for (i = 0; i < TSI148_MAX_SLAVE; i++) {
Markus Elfring6d011dd2017-08-25 11:55:03 +02002410 slave_image = kmalloc(sizeof(*slave_image), GFP_KERNEL);
Markus Elfringa75dc632017-08-25 12:00:17 +02002411 if (!slave_image) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002412 retval = -ENOMEM;
2413 goto err_slave;
2414 }
2415 slave_image->parent = tsi148_bridge;
Emilio G. Cota886953e2010-11-12 11:14:07 +00002416 mutex_init(&slave_image->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002417 slave_image->locked = 0;
2418 slave_image->number = i;
2419 slave_image->address_attr = VME_A16 | VME_A24 | VME_A32 |
Martyn Welch08e03c22015-02-26 18:53:11 +03002420 VME_A64;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002421 slave_image->cycle_attr = VME_SCT | VME_BLT | VME_MBLT |
2422 VME_2eVME | VME_2eSST | VME_2eSSTB | VME_2eSST160 |
2423 VME_2eSST267 | VME_2eSST320 | VME_SUPER | VME_USER |
2424 VME_PROG | VME_DATA;
Emilio G. Cota886953e2010-11-12 11:14:07 +00002425 list_add_tail(&slave_image->list,
2426 &tsi148_bridge->slave_resources);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002427 }
2428
2429 /* Add dma engines to list */
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002430 for (i = 0; i < TSI148_MAX_DMA; i++) {
Markus Elfring6d011dd2017-08-25 11:55:03 +02002431 dma_ctrlr = kmalloc(sizeof(*dma_ctrlr), GFP_KERNEL);
Markus Elfringa75dc632017-08-25 12:00:17 +02002432 if (!dma_ctrlr) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002433 retval = -ENOMEM;
2434 goto err_dma;
2435 }
2436 dma_ctrlr->parent = tsi148_bridge;
Emilio G. Cota886953e2010-11-12 11:14:07 +00002437 mutex_init(&dma_ctrlr->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002438 dma_ctrlr->locked = 0;
2439 dma_ctrlr->number = i;
Martyn Welch4f723df2010-02-18 15:12:58 +00002440 dma_ctrlr->route_attr = VME_DMA_VME_TO_MEM |
2441 VME_DMA_MEM_TO_VME | VME_DMA_VME_TO_VME |
2442 VME_DMA_MEM_TO_MEM | VME_DMA_PATTERN_TO_VME |
2443 VME_DMA_PATTERN_TO_MEM;
Emilio G. Cota886953e2010-11-12 11:14:07 +00002444 INIT_LIST_HEAD(&dma_ctrlr->pending);
2445 INIT_LIST_HEAD(&dma_ctrlr->running);
2446 list_add_tail(&dma_ctrlr->list,
2447 &tsi148_bridge->dma_resources);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002448 }
2449
Martyn Welch42fb5032009-08-11 17:44:56 +01002450 /* Add location monitor to list */
Markus Elfring6d011dd2017-08-25 11:55:03 +02002451 lm = kmalloc(sizeof(*lm), GFP_KERNEL);
Markus Elfringa75dc632017-08-25 12:00:17 +02002452 if (!lm) {
Martyn Welch42fb5032009-08-11 17:44:56 +01002453 retval = -ENOMEM;
2454 goto err_lm;
2455 }
2456 lm->parent = tsi148_bridge;
Emilio G. Cota886953e2010-11-12 11:14:07 +00002457 mutex_init(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01002458 lm->locked = 0;
2459 lm->number = 1;
2460 lm->monitors = 4;
Emilio G. Cota886953e2010-11-12 11:14:07 +00002461 list_add_tail(&lm->list, &tsi148_bridge->lm_resources);
Martyn Welch42fb5032009-08-11 17:44:56 +01002462
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002463 tsi148_bridge->slave_get = tsi148_slave_get;
2464 tsi148_bridge->slave_set = tsi148_slave_set;
2465 tsi148_bridge->master_get = tsi148_master_get;
2466 tsi148_bridge->master_set = tsi148_master_set;
2467 tsi148_bridge->master_read = tsi148_master_read;
2468 tsi148_bridge->master_write = tsi148_master_write;
2469 tsi148_bridge->master_rmw = tsi148_master_rmw;
2470 tsi148_bridge->dma_list_add = tsi148_dma_list_add;
2471 tsi148_bridge->dma_list_exec = tsi148_dma_list_exec;
2472 tsi148_bridge->dma_list_empty = tsi148_dma_list_empty;
Martyn Welchc813f592009-10-29 16:34:54 +00002473 tsi148_bridge->irq_set = tsi148_irq_set;
2474 tsi148_bridge->irq_generate = tsi148_irq_generate;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002475 tsi148_bridge->lm_set = tsi148_lm_set;
2476 tsi148_bridge->lm_get = tsi148_lm_get;
2477 tsi148_bridge->lm_attach = tsi148_lm_attach;
2478 tsi148_bridge->lm_detach = tsi148_lm_detach;
2479 tsi148_bridge->slot_get = tsi148_slot_get;
Manohar Vanga7f58f022011-08-10 11:33:46 +02002480 tsi148_bridge->alloc_consistent = tsi148_alloc_consistent;
2481 tsi148_bridge->free_consistent = tsi148_free_consistent;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002482
Martyn Welch29848ac2010-02-18 15:13:05 +00002483 data = ioread32be(tsi148_device->base + TSI148_LCSR_VSTAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002484 dev_info(&pdev->dev, "Board is%s the VME system controller\n",
Martyn Welch79463282010-03-22 14:58:57 +00002485 (data & TSI148_LCSR_VSTAT_SCONS) ? "" : " not");
Martyn Welch29848ac2010-02-18 15:13:05 +00002486 if (!geoid)
Martyn Welch638f1992009-12-15 08:42:49 +00002487 dev_info(&pdev->dev, "VME geographical address is %d\n",
2488 data & TSI148_LCSR_VSTAT_GA_M);
Martyn Welch29848ac2010-02-18 15:13:05 +00002489 else
Martyn Welch638f1992009-12-15 08:42:49 +00002490 dev_info(&pdev->dev, "VME geographical address is set to %d\n",
2491 geoid);
Martyn Welch29848ac2010-02-18 15:13:05 +00002492
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002493 dev_info(&pdev->dev, "VME Write and flush and error check is %s\n",
2494 err_chk ? "enabled" : "disabled");
2495
Wei Yongjun0686ab72013-06-19 10:42:35 +08002496 retval = tsi148_crcsr_init(tsi148_bridge, pdev);
2497 if (retval) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002498 dev_err(&pdev->dev, "CR/CSR configuration failed.\n");
2499 goto err_crcsr;
Martyn Welch48397372010-03-22 14:58:43 +00002500 }
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002501
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002502 retval = vme_register_bridge(tsi148_bridge);
2503 if (retval != 0) {
2504 dev_err(&pdev->dev, "Chip Registration failed.\n");
2505 goto err_reg;
2506 }
2507
Martyn Welch29848ac2010-02-18 15:13:05 +00002508 pci_set_drvdata(pdev, tsi148_bridge);
2509
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002510 /* Clear VME bus "board fail", and "power-up reset" lines */
Martyn Welch29848ac2010-02-18 15:13:05 +00002511 data = ioread32be(tsi148_device->base + TSI148_LCSR_VSTAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002512 data &= ~TSI148_LCSR_VSTAT_BRDFL;
2513 data |= TSI148_LCSR_VSTAT_CPURST;
Martyn Welch29848ac2010-02-18 15:13:05 +00002514 iowrite32be(data, tsi148_device->base + TSI148_LCSR_VSTAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002515
2516 return 0;
2517
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002518err_reg:
Martyn Welch29848ac2010-02-18 15:13:05 +00002519 tsi148_crcsr_exit(tsi148_bridge, pdev);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002520err_crcsr:
Martyn Welch42fb5032009-08-11 17:44:56 +01002521err_lm:
2522 /* resources are stored in link list */
Wei Yongjunb49c32b2012-08-21 12:19:01 +08002523 list_for_each_safe(pos, n, &tsi148_bridge->lm_resources) {
Martyn Welch42fb5032009-08-11 17:44:56 +01002524 lm = list_entry(pos, struct vme_lm_resource, list);
2525 list_del(pos);
2526 kfree(lm);
2527 }
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002528err_dma:
2529 /* resources are stored in link list */
Wei Yongjunb49c32b2012-08-21 12:19:01 +08002530 list_for_each_safe(pos, n, &tsi148_bridge->dma_resources) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002531 dma_ctrlr = list_entry(pos, struct vme_dma_resource, list);
2532 list_del(pos);
2533 kfree(dma_ctrlr);
2534 }
2535err_slave:
2536 /* resources are stored in link list */
Wei Yongjunb49c32b2012-08-21 12:19:01 +08002537 list_for_each_safe(pos, n, &tsi148_bridge->slave_resources) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002538 slave_image = list_entry(pos, struct vme_slave_resource, list);
2539 list_del(pos);
2540 kfree(slave_image);
2541 }
2542err_master:
2543 /* resources are stored in link list */
Wei Yongjunb49c32b2012-08-21 12:19:01 +08002544 list_for_each_safe(pos, n, &tsi148_bridge->master_resources) {
Martyn Welch79463282010-03-22 14:58:57 +00002545 master_image = list_entry(pos, struct vme_master_resource,
2546 list);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002547 list_del(pos);
2548 kfree(master_image);
2549 }
2550
Emilio G. Cotaa82ad052010-11-12 11:14:47 +00002551 tsi148_irq_exit(tsi148_bridge, pdev);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002552err_irq:
2553err_test:
Martyn Welch29848ac2010-02-18 15:13:05 +00002554 iounmap(tsi148_device->base);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002555err_remap:
2556 pci_release_regions(pdev);
2557err_resource:
2558 pci_disable_device(pdev);
2559err_enable:
Martyn Welch29848ac2010-02-18 15:13:05 +00002560 kfree(tsi148_device);
2561err_driver:
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002562 kfree(tsi148_bridge);
2563err_struct:
2564 return retval;
2565
2566}
2567
2568static void tsi148_remove(struct pci_dev *pdev)
2569{
2570 struct list_head *pos = NULL;
Emilio G. Cotab558ba22010-11-12 11:14:34 +00002571 struct list_head *tmplist;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002572 struct vme_master_resource *master_image;
2573 struct vme_slave_resource *slave_image;
2574 struct vme_dma_resource *dma_ctrlr;
2575 int i;
Martyn Welch29848ac2010-02-18 15:13:05 +00002576 struct tsi148_driver *bridge;
2577 struct vme_bridge *tsi148_bridge = pci_get_drvdata(pdev);
2578
2579 bridge = tsi148_bridge->driver_priv;
2580
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002581
2582 dev_dbg(&pdev->dev, "Driver is being unloaded.\n");
2583
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002584 /*
2585 * Shutdown all inbound and outbound windows.
2586 */
2587 for (i = 0; i < 8; i++) {
Martyn Welch29848ac2010-02-18 15:13:05 +00002588 iowrite32be(0, bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002589 TSI148_LCSR_OFFSET_ITAT);
Martyn Welch29848ac2010-02-18 15:13:05 +00002590 iowrite32be(0, bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002591 TSI148_LCSR_OFFSET_OTAT);
2592 }
2593
2594 /*
2595 * Shutdown Location monitor.
2596 */
Martyn Welch29848ac2010-02-18 15:13:05 +00002597 iowrite32be(0, bridge->base + TSI148_LCSR_LMAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002598
2599 /*
2600 * Shutdown CRG map.
2601 */
Martyn Welch29848ac2010-02-18 15:13:05 +00002602 iowrite32be(0, bridge->base + TSI148_LCSR_CSRAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002603
2604 /*
2605 * Clear error status.
2606 */
Martyn Welch29848ac2010-02-18 15:13:05 +00002607 iowrite32be(0xFFFFFFFF, bridge->base + TSI148_LCSR_EDPAT);
2608 iowrite32be(0xFFFFFFFF, bridge->base + TSI148_LCSR_VEAT);
2609 iowrite32be(0x07000700, bridge->base + TSI148_LCSR_PSTAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002610
2611 /*
2612 * Remove VIRQ interrupt (if any)
2613 */
Martyn Welch29848ac2010-02-18 15:13:05 +00002614 if (ioread32be(bridge->base + TSI148_LCSR_VICR) & 0x800)
2615 iowrite32be(0x8000, bridge->base + TSI148_LCSR_VICR);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002616
2617 /*
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002618 * Map all Interrupts to PCI INTA
2619 */
Martyn Welch29848ac2010-02-18 15:13:05 +00002620 iowrite32be(0x0, bridge->base + TSI148_LCSR_INTM1);
2621 iowrite32be(0x0, bridge->base + TSI148_LCSR_INTM2);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002622
Emilio G. Cotaa82ad052010-11-12 11:14:47 +00002623 tsi148_irq_exit(tsi148_bridge, pdev);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002624
2625 vme_unregister_bridge(tsi148_bridge);
2626
Martyn Welch29848ac2010-02-18 15:13:05 +00002627 tsi148_crcsr_exit(tsi148_bridge, pdev);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002628
2629 /* resources are stored in link list */
Emilio G. Cotab558ba22010-11-12 11:14:34 +00002630 list_for_each_safe(pos, tmplist, &tsi148_bridge->dma_resources) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002631 dma_ctrlr = list_entry(pos, struct vme_dma_resource, list);
2632 list_del(pos);
2633 kfree(dma_ctrlr);
2634 }
2635
2636 /* resources are stored in link list */
Emilio G. Cotab558ba22010-11-12 11:14:34 +00002637 list_for_each_safe(pos, tmplist, &tsi148_bridge->slave_resources) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002638 slave_image = list_entry(pos, struct vme_slave_resource, list);
2639 list_del(pos);
2640 kfree(slave_image);
2641 }
2642
2643 /* resources are stored in link list */
Emilio G. Cotab558ba22010-11-12 11:14:34 +00002644 list_for_each_safe(pos, tmplist, &tsi148_bridge->master_resources) {
Martyn Welch638f1992009-12-15 08:42:49 +00002645 master_image = list_entry(pos, struct vme_master_resource,
2646 list);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002647 list_del(pos);
2648 kfree(master_image);
2649 }
2650
Martyn Welch29848ac2010-02-18 15:13:05 +00002651 iounmap(bridge->base);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002652
2653 pci_release_regions(pdev);
2654
2655 pci_disable_device(pdev);
2656
Martyn Welch29848ac2010-02-18 15:13:05 +00002657 kfree(tsi148_bridge->driver_priv);
2658
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002659 kfree(tsi148_bridge);
2660}
2661
Wei Yongjun01c07142012-10-18 23:12:50 +08002662module_pci_driver(tsi148_driver);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002663
2664MODULE_PARM_DESC(err_chk, "Check for VME errors on reads and writes");
2665module_param(err_chk, bool, 0);
2666
Martyn Welch638f1992009-12-15 08:42:49 +00002667MODULE_PARM_DESC(geoid, "Override geographical addressing");
2668module_param(geoid, int, 0);
2669
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002670MODULE_DESCRIPTION("VME driver for the Tundra Tempe VME bridge");
2671MODULE_LICENSE("GPL");