blob: 60c4933ca0c8791f91b974b570a8afc548acdba4 [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
53static struct pci_driver tsi148_driver = {
54 .name = driver_name,
55 .id_table = tsi148_ids,
56 .probe = tsi148_probe,
57 .remove = tsi148_remove,
58};
59
60static void reg_join(unsigned int high, unsigned int low,
61 unsigned long long *variable)
62{
63 *variable = (unsigned long long)high << 32;
64 *variable |= (unsigned long long)low;
65}
66
67static void reg_split(unsigned long long variable, unsigned int *high,
68 unsigned int *low)
69{
70 *low = (unsigned int)variable & 0xFFFFFFFF;
71 *high = (unsigned int)(variable >> 32);
72}
73
74/*
75 * Wakes up DMA queue.
76 */
Martyn Welch29848ac2010-02-18 15:13:05 +000077static u32 tsi148_DMA_irqhandler(struct tsi148_driver *bridge,
78 int channel_mask)
Martyn Welchd22b8ed2009-07-31 09:28:17 +010079{
80 u32 serviced = 0;
81
82 if (channel_mask & TSI148_LCSR_INTS_DMA0S) {
Emilio G. Cota886953e2010-11-12 11:14:07 +000083 wake_up(&bridge->dma_queue[0]);
Martyn Welchd22b8ed2009-07-31 09:28:17 +010084 serviced |= TSI148_LCSR_INTC_DMA0C;
85 }
86 if (channel_mask & TSI148_LCSR_INTS_DMA1S) {
Emilio G. Cota886953e2010-11-12 11:14:07 +000087 wake_up(&bridge->dma_queue[1]);
Martyn Welchd22b8ed2009-07-31 09:28:17 +010088 serviced |= TSI148_LCSR_INTC_DMA1C;
89 }
90
91 return serviced;
92}
93
94/*
95 * Wake up location monitor queue
96 */
Martyn Welch29848ac2010-02-18 15:13:05 +000097static u32 tsi148_LM_irqhandler(struct tsi148_driver *bridge, u32 stat)
Martyn Welchd22b8ed2009-07-31 09:28:17 +010098{
99 int i;
100 u32 serviced = 0;
101
102 for (i = 0; i < 4; i++) {
Martyn Welch79463282010-03-22 14:58:57 +0000103 if (stat & TSI148_LCSR_INTS_LMS[i]) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100104 /* We only enable interrupts if the callback is set */
Martyn Welch29848ac2010-02-18 15:13:05 +0000105 bridge->lm_callback[i](i);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100106 serviced |= TSI148_LCSR_INTC_LMC[i];
107 }
108 }
109
110 return serviced;
111}
112
113/*
114 * Wake up mail box queue.
115 *
116 * XXX This functionality is not exposed up though API.
117 */
Martyn Welch48d93562010-03-22 14:58:50 +0000118static u32 tsi148_MB_irqhandler(struct vme_bridge *tsi148_bridge, u32 stat)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100119{
120 int i;
121 u32 val;
122 u32 serviced = 0;
Martyn Welch48d93562010-03-22 14:58:50 +0000123 struct tsi148_driver *bridge;
124
125 bridge = tsi148_bridge->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100126
127 for (i = 0; i < 4; i++) {
Martyn Welch79463282010-03-22 14:58:57 +0000128 if (stat & TSI148_LCSR_INTS_MBS[i]) {
Martyn Welch29848ac2010-02-18 15:13:05 +0000129 val = ioread32be(bridge->base + TSI148_GCSR_MBOX[i]);
Martyn Welch48d93562010-03-22 14:58:50 +0000130 dev_err(tsi148_bridge->parent, "VME Mailbox %d received"
131 ": 0x%x\n", i, val);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100132 serviced |= TSI148_LCSR_INTC_MBC[i];
133 }
134 }
135
136 return serviced;
137}
138
139/*
140 * Display error & status message when PERR (PCI) exception interrupt occurs.
141 */
Martyn Welch48d93562010-03-22 14:58:50 +0000142static u32 tsi148_PERR_irqhandler(struct vme_bridge *tsi148_bridge)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100143{
Martyn Welch48d93562010-03-22 14:58:50 +0000144 struct tsi148_driver *bridge;
145
146 bridge = tsi148_bridge->driver_priv;
147
148 dev_err(tsi148_bridge->parent, "PCI Exception at address: 0x%08x:%08x, "
149 "attributes: %08x\n",
Martyn Welch29848ac2010-02-18 15:13:05 +0000150 ioread32be(bridge->base + TSI148_LCSR_EDPAU),
151 ioread32be(bridge->base + TSI148_LCSR_EDPAL),
Martyn Welch48d93562010-03-22 14:58:50 +0000152 ioread32be(bridge->base + TSI148_LCSR_EDPAT));
153
154 dev_err(tsi148_bridge->parent, "PCI-X attribute reg: %08x, PCI-X split "
155 "completion reg: %08x\n",
Martyn Welch29848ac2010-02-18 15:13:05 +0000156 ioread32be(bridge->base + TSI148_LCSR_EDPXA),
Martyn Welch48d93562010-03-22 14:58:50 +0000157 ioread32be(bridge->base + TSI148_LCSR_EDPXS));
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100158
Martyn Welch29848ac2010-02-18 15:13:05 +0000159 iowrite32be(TSI148_LCSR_EDPAT_EDPCL, bridge->base + TSI148_LCSR_EDPAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100160
161 return TSI148_LCSR_INTC_PERRC;
162}
163
164/*
165 * Save address and status when VME error interrupt occurs.
166 */
Martyn Welch29848ac2010-02-18 15:13:05 +0000167static u32 tsi148_VERR_irqhandler(struct vme_bridge *tsi148_bridge)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100168{
169 unsigned int error_addr_high, error_addr_low;
170 unsigned long long error_addr;
171 u32 error_attrib;
Martyn Welch29848ac2010-02-18 15:13:05 +0000172 struct tsi148_driver *bridge;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100173
Martyn Welch29848ac2010-02-18 15:13:05 +0000174 bridge = tsi148_bridge->driver_priv;
175
176 error_addr_high = ioread32be(bridge->base + TSI148_LCSR_VEAU);
177 error_addr_low = ioread32be(bridge->base + TSI148_LCSR_VEAL);
178 error_attrib = ioread32be(bridge->base + TSI148_LCSR_VEAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100179
180 reg_join(error_addr_high, error_addr_low, &error_addr);
181
182 /* Check for exception register overflow (we have lost error data) */
Martyn Welch79463282010-03-22 14:58:57 +0000183 if (error_attrib & TSI148_LCSR_VEAT_VEOF) {
Martyn Welch48d93562010-03-22 14:58:50 +0000184 dev_err(tsi148_bridge->parent, "VME Bus Exception Overflow "
185 "Occurred\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100186 }
187
Dmitry Kalinkine2c63932015-09-18 02:01:42 +0300188 if (err_chk)
189 vme_bus_error_handler(tsi148_bridge, error_addr, error_attrib);
190 else
Martyn Welche31c51e2013-06-11 11:20:17 +0100191 dev_err(tsi148_bridge->parent,
192 "VME Bus Error at address: 0x%llx, attributes: %08x\n",
193 error_addr, error_attrib);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100194
195 /* Clear Status */
Martyn Welch29848ac2010-02-18 15:13:05 +0000196 iowrite32be(TSI148_LCSR_VEAT_VESCL, bridge->base + TSI148_LCSR_VEAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100197
198 return TSI148_LCSR_INTC_VERRC;
199}
200
201/*
202 * Wake up IACK queue.
203 */
Martyn Welch29848ac2010-02-18 15:13:05 +0000204static u32 tsi148_IACK_irqhandler(struct tsi148_driver *bridge)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100205{
Emilio G. Cota886953e2010-11-12 11:14:07 +0000206 wake_up(&bridge->iack_queue);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100207
208 return TSI148_LCSR_INTC_IACKC;
209}
210
211/*
212 * Calling VME bus interrupt callback if provided.
213 */
Martyn Welch29848ac2010-02-18 15:13:05 +0000214static u32 tsi148_VIRQ_irqhandler(struct vme_bridge *tsi148_bridge,
215 u32 stat)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100216{
217 int vec, i, serviced = 0;
Martyn Welch29848ac2010-02-18 15:13:05 +0000218 struct tsi148_driver *bridge;
219
220 bridge = tsi148_bridge->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100221
222 for (i = 7; i > 0; i--) {
223 if (stat & (1 << i)) {
224 /*
Martyn Welch79463282010-03-22 14:58:57 +0000225 * Note: Even though the registers are defined as
226 * 32-bits in the spec, we only want to issue 8-bit
227 * IACK cycles on the bus, read from offset 3.
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100228 */
Martyn Welch29848ac2010-02-18 15:13:05 +0000229 vec = ioread8(bridge->base + TSI148_LCSR_VIACK[i] + 3);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100230
Martyn Welchc813f592009-10-29 16:34:54 +0000231 vme_irq_handler(tsi148_bridge, i, vec);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100232
233 serviced |= (1 << i);
234 }
235 }
236
237 return serviced;
238}
239
240/*
241 * Top level interrupt handler. Clears appropriate interrupt status bits and
242 * then calls appropriate sub handler(s).
243 */
Martyn Welch29848ac2010-02-18 15:13:05 +0000244static irqreturn_t tsi148_irqhandler(int irq, void *ptr)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100245{
246 u32 stat, enable, serviced = 0;
Martyn Welch29848ac2010-02-18 15:13:05 +0000247 struct vme_bridge *tsi148_bridge;
248 struct tsi148_driver *bridge;
249
250 tsi148_bridge = ptr;
251
252 bridge = tsi148_bridge->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100253
254 /* Determine which interrupts are unmasked and set */
Martyn Welch29848ac2010-02-18 15:13:05 +0000255 enable = ioread32be(bridge->base + TSI148_LCSR_INTEO);
256 stat = ioread32be(bridge->base + TSI148_LCSR_INTS);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100257
258 /* Only look at unmasked interrupts */
259 stat &= enable;
260
Martyn Welch79463282010-03-22 14:58:57 +0000261 if (unlikely(!stat))
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100262 return IRQ_NONE;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100263
264 /* Call subhandlers as appropriate */
265 /* DMA irqs */
266 if (stat & (TSI148_LCSR_INTS_DMA1S | TSI148_LCSR_INTS_DMA0S))
Martyn Welch29848ac2010-02-18 15:13:05 +0000267 serviced |= tsi148_DMA_irqhandler(bridge, stat);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100268
269 /* Location monitor irqs */
270 if (stat & (TSI148_LCSR_INTS_LM3S | TSI148_LCSR_INTS_LM2S |
271 TSI148_LCSR_INTS_LM1S | TSI148_LCSR_INTS_LM0S))
Martyn Welch29848ac2010-02-18 15:13:05 +0000272 serviced |= tsi148_LM_irqhandler(bridge, stat);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100273
274 /* Mail box irqs */
275 if (stat & (TSI148_LCSR_INTS_MB3S | TSI148_LCSR_INTS_MB2S |
276 TSI148_LCSR_INTS_MB1S | TSI148_LCSR_INTS_MB0S))
Martyn Welch48d93562010-03-22 14:58:50 +0000277 serviced |= tsi148_MB_irqhandler(tsi148_bridge, stat);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100278
279 /* PCI bus error */
280 if (stat & TSI148_LCSR_INTS_PERRS)
Martyn Welch48d93562010-03-22 14:58:50 +0000281 serviced |= tsi148_PERR_irqhandler(tsi148_bridge);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100282
283 /* VME bus error */
284 if (stat & TSI148_LCSR_INTS_VERRS)
Martyn Welch29848ac2010-02-18 15:13:05 +0000285 serviced |= tsi148_VERR_irqhandler(tsi148_bridge);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100286
287 /* IACK irq */
288 if (stat & TSI148_LCSR_INTS_IACKS)
Martyn Welch29848ac2010-02-18 15:13:05 +0000289 serviced |= tsi148_IACK_irqhandler(bridge);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100290
291 /* VME bus irqs */
292 if (stat & (TSI148_LCSR_INTS_IRQ7S | TSI148_LCSR_INTS_IRQ6S |
293 TSI148_LCSR_INTS_IRQ5S | TSI148_LCSR_INTS_IRQ4S |
294 TSI148_LCSR_INTS_IRQ3S | TSI148_LCSR_INTS_IRQ2S |
295 TSI148_LCSR_INTS_IRQ1S))
Martyn Welch29848ac2010-02-18 15:13:05 +0000296 serviced |= tsi148_VIRQ_irqhandler(tsi148_bridge, stat);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100297
298 /* Clear serviced interrupts */
Martyn Welch29848ac2010-02-18 15:13:05 +0000299 iowrite32be(serviced, bridge->base + TSI148_LCSR_INTC);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100300
301 return IRQ_HANDLED;
302}
303
Martyn Welch29848ac2010-02-18 15:13:05 +0000304static int tsi148_irq_init(struct vme_bridge *tsi148_bridge)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100305{
306 int result;
307 unsigned int tmp;
308 struct pci_dev *pdev;
Martyn Welch29848ac2010-02-18 15:13:05 +0000309 struct tsi148_driver *bridge;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100310
Aaron Sierra177581fa2014-04-03 14:48:27 -0500311 pdev = to_pci_dev(tsi148_bridge->parent);
Martyn Welch29848ac2010-02-18 15:13:05 +0000312
313 bridge = tsi148_bridge->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100314
315 /* Initialise list for VME bus errors */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000316 INIT_LIST_HEAD(&tsi148_bridge->vme_errors);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100317
Emilio G. Cota886953e2010-11-12 11:14:07 +0000318 mutex_init(&tsi148_bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +0000319
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100320 result = request_irq(pdev->irq,
321 tsi148_irqhandler,
322 IRQF_SHARED,
Martyn Welch29848ac2010-02-18 15:13:05 +0000323 driver_name, tsi148_bridge);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100324 if (result) {
Martyn Welch48d93562010-03-22 14:58:50 +0000325 dev_err(tsi148_bridge->parent, "Can't get assigned pci irq "
326 "vector %02X\n", pdev->irq);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100327 return result;
328 }
329
330 /* Enable and unmask interrupts */
331 tmp = TSI148_LCSR_INTEO_DMA1EO | TSI148_LCSR_INTEO_DMA0EO |
332 TSI148_LCSR_INTEO_MB3EO | TSI148_LCSR_INTEO_MB2EO |
333 TSI148_LCSR_INTEO_MB1EO | TSI148_LCSR_INTEO_MB0EO |
334 TSI148_LCSR_INTEO_PERREO | TSI148_LCSR_INTEO_VERREO |
335 TSI148_LCSR_INTEO_IACKEO;
336
Martyn Welch29848ac2010-02-18 15:13:05 +0000337 /* This leaves the following interrupts masked.
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100338 * TSI148_LCSR_INTEO_VIEEO
339 * TSI148_LCSR_INTEO_SYSFLEO
340 * TSI148_LCSR_INTEO_ACFLEO
341 */
342
343 /* Don't enable Location Monitor interrupts here - they will be
344 * enabled when the location monitors are properly configured and
345 * a callback has been attached.
346 * TSI148_LCSR_INTEO_LM0EO
347 * TSI148_LCSR_INTEO_LM1EO
348 * TSI148_LCSR_INTEO_LM2EO
349 * TSI148_LCSR_INTEO_LM3EO
350 */
351
352 /* Don't enable VME interrupts until we add a handler, else the board
353 * will respond to it and we don't want that unless it knows how to
354 * properly deal with it.
355 * TSI148_LCSR_INTEO_IRQ7EO
356 * TSI148_LCSR_INTEO_IRQ6EO
357 * TSI148_LCSR_INTEO_IRQ5EO
358 * TSI148_LCSR_INTEO_IRQ4EO
359 * TSI148_LCSR_INTEO_IRQ3EO
360 * TSI148_LCSR_INTEO_IRQ2EO
361 * TSI148_LCSR_INTEO_IRQ1EO
362 */
363
364 iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEO);
365 iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEN);
366
367 return 0;
368}
369
Emilio G. Cotaa82ad052010-11-12 11:14:47 +0000370static void tsi148_irq_exit(struct vme_bridge *tsi148_bridge,
371 struct pci_dev *pdev)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100372{
Emilio G. Cotaa82ad052010-11-12 11:14:47 +0000373 struct tsi148_driver *bridge = tsi148_bridge->driver_priv;
374
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100375 /* Turn off interrupts */
Martyn Welch29848ac2010-02-18 15:13:05 +0000376 iowrite32be(0x0, bridge->base + TSI148_LCSR_INTEO);
377 iowrite32be(0x0, bridge->base + TSI148_LCSR_INTEN);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100378
379 /* Clear all interrupts */
Martyn Welch29848ac2010-02-18 15:13:05 +0000380 iowrite32be(0xFFFFFFFF, bridge->base + TSI148_LCSR_INTC);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100381
382 /* Detach interrupt handler */
Emilio G. Cotaa82ad052010-11-12 11:14:47 +0000383 free_irq(pdev->irq, tsi148_bridge);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100384}
385
386/*
387 * Check to see if an IACk has been received, return true (1) or false (0).
388 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +0000389static int tsi148_iack_received(struct tsi148_driver *bridge)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100390{
391 u32 tmp;
392
Martyn Welch29848ac2010-02-18 15:13:05 +0000393 tmp = ioread32be(bridge->base + TSI148_LCSR_VICR);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100394
395 if (tmp & TSI148_LCSR_VICR_IRQS)
396 return 0;
397 else
398 return 1;
399}
400
401/*
Martyn Welchc813f592009-10-29 16:34:54 +0000402 * Configure VME interrupt
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100403 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +0000404static void tsi148_irq_set(struct vme_bridge *tsi148_bridge, int level,
Martyn Welch29848ac2010-02-18 15:13:05 +0000405 int state, int sync)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100406{
Martyn Welch75155022009-08-11 13:50:49 +0100407 struct pci_dev *pdev;
Martyn Welchc813f592009-10-29 16:34:54 +0000408 u32 tmp;
Martyn Welch29848ac2010-02-18 15:13:05 +0000409 struct tsi148_driver *bridge;
410
411 bridge = tsi148_bridge->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100412
Martyn Welchc813f592009-10-29 16:34:54 +0000413 /* We need to do the ordering differently for enabling and disabling */
414 if (state == 0) {
Martyn Welch29848ac2010-02-18 15:13:05 +0000415 tmp = ioread32be(bridge->base + TSI148_LCSR_INTEN);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100416 tmp &= ~TSI148_LCSR_INTEN_IRQEN[level - 1];
Martyn Welch29848ac2010-02-18 15:13:05 +0000417 iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEN);
Martyn Welchdf455172009-08-05 17:38:31 +0100418
Martyn Welch29848ac2010-02-18 15:13:05 +0000419 tmp = ioread32be(bridge->base + TSI148_LCSR_INTEO);
Martyn Welchdf455172009-08-05 17:38:31 +0100420 tmp &= ~TSI148_LCSR_INTEO_IRQEO[level - 1];
Martyn Welch29848ac2010-02-18 15:13:05 +0000421 iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEO);
Martyn Welch75155022009-08-11 13:50:49 +0100422
Martyn Welchc813f592009-10-29 16:34:54 +0000423 if (sync != 0) {
Aaron Sierra177581fa2014-04-03 14:48:27 -0500424 pdev = to_pci_dev(tsi148_bridge->parent);
Martyn Welchc813f592009-10-29 16:34:54 +0000425 synchronize_irq(pdev->irq);
426 }
427 } else {
Martyn Welch29848ac2010-02-18 15:13:05 +0000428 tmp = ioread32be(bridge->base + TSI148_LCSR_INTEO);
Martyn Welchc813f592009-10-29 16:34:54 +0000429 tmp |= TSI148_LCSR_INTEO_IRQEO[level - 1];
Martyn Welch29848ac2010-02-18 15:13:05 +0000430 iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEO);
Martyn Welchc813f592009-10-29 16:34:54 +0000431
Martyn Welch29848ac2010-02-18 15:13:05 +0000432 tmp = ioread32be(bridge->base + TSI148_LCSR_INTEN);
Martyn Welchc813f592009-10-29 16:34:54 +0000433 tmp |= TSI148_LCSR_INTEN_IRQEN[level - 1];
Martyn Welch29848ac2010-02-18 15:13:05 +0000434 iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEN);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100435 }
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100436}
437
438/*
439 * Generate a VME bus interrupt at the requested level & vector. Wait for
440 * interrupt to be acked.
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100441 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +0000442static int tsi148_irq_generate(struct vme_bridge *tsi148_bridge, int level,
443 int statid)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100444{
445 u32 tmp;
Martyn Welch29848ac2010-02-18 15:13:05 +0000446 struct tsi148_driver *bridge;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100447
Martyn Welch29848ac2010-02-18 15:13:05 +0000448 bridge = tsi148_bridge->driver_priv;
449
Emilio G. Cota886953e2010-11-12 11:14:07 +0000450 mutex_lock(&bridge->vme_int);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100451
452 /* Read VICR register */
Martyn Welch29848ac2010-02-18 15:13:05 +0000453 tmp = ioread32be(bridge->base + TSI148_LCSR_VICR);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100454
455 /* Set Status/ID */
456 tmp = (tmp & ~TSI148_LCSR_VICR_STID_M) |
457 (statid & TSI148_LCSR_VICR_STID_M);
Martyn Welch29848ac2010-02-18 15:13:05 +0000458 iowrite32be(tmp, bridge->base + TSI148_LCSR_VICR);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100459
460 /* Assert VMEbus IRQ */
461 tmp = tmp | TSI148_LCSR_VICR_IRQL[level];
Martyn Welch29848ac2010-02-18 15:13:05 +0000462 iowrite32be(tmp, bridge->base + TSI148_LCSR_VICR);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100463
464 /* XXX Consider implementing a timeout? */
Martyn Welch29848ac2010-02-18 15:13:05 +0000465 wait_event_interruptible(bridge->iack_queue,
466 tsi148_iack_received(bridge));
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100467
Emilio G. Cota886953e2010-11-12 11:14:07 +0000468 mutex_unlock(&bridge->vme_int);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100469
470 return 0;
471}
472
473/*
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100474 * Initialize a slave window with the requested attributes.
475 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +0000476static int tsi148_slave_set(struct vme_slave_resource *image, int enabled,
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100477 unsigned long long vme_base, unsigned long long size,
Martyn Welch6af04b02011-12-01 17:06:29 +0000478 dma_addr_t pci_base, u32 aspace, u32 cycle)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100479{
480 unsigned int i, addr = 0, granularity = 0;
481 unsigned int temp_ctl = 0;
482 unsigned int vme_base_low, vme_base_high;
483 unsigned int vme_bound_low, vme_bound_high;
484 unsigned int pci_offset_low, pci_offset_high;
485 unsigned long long vme_bound, pci_offset;
Martyn Welch48d93562010-03-22 14:58:50 +0000486 struct vme_bridge *tsi148_bridge;
Martyn Welch29848ac2010-02-18 15:13:05 +0000487 struct tsi148_driver *bridge;
488
Martyn Welch48d93562010-03-22 14:58:50 +0000489 tsi148_bridge = image->parent;
490 bridge = tsi148_bridge->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100491
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100492 i = image->number;
493
494 switch (aspace) {
495 case VME_A16:
496 granularity = 0x10;
497 addr |= TSI148_LCSR_ITAT_AS_A16;
498 break;
499 case VME_A24:
500 granularity = 0x1000;
501 addr |= TSI148_LCSR_ITAT_AS_A24;
502 break;
503 case VME_A32:
504 granularity = 0x10000;
505 addr |= TSI148_LCSR_ITAT_AS_A32;
506 break;
507 case VME_A64:
508 granularity = 0x10000;
509 addr |= TSI148_LCSR_ITAT_AS_A64;
510 break;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100511 default:
Martyn Welch48d93562010-03-22 14:58:50 +0000512 dev_err(tsi148_bridge->parent, "Invalid address space\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100513 return -EINVAL;
514 break;
515 }
516
517 /* Convert 64-bit variables to 2x 32-bit variables */
518 reg_split(vme_base, &vme_base_high, &vme_base_low);
519
520 /*
521 * Bound address is a valid address for the window, adjust
522 * accordingly
523 */
524 vme_bound = vme_base + size - granularity;
525 reg_split(vme_bound, &vme_bound_high, &vme_bound_low);
526 pci_offset = (unsigned long long)pci_base - vme_base;
527 reg_split(pci_offset, &pci_offset_high, &pci_offset_low);
528
529 if (vme_base_low & (granularity - 1)) {
Martyn Welch48d93562010-03-22 14:58:50 +0000530 dev_err(tsi148_bridge->parent, "Invalid VME base alignment\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100531 return -EINVAL;
532 }
533 if (vme_bound_low & (granularity - 1)) {
Martyn Welch48d93562010-03-22 14:58:50 +0000534 dev_err(tsi148_bridge->parent, "Invalid VME bound alignment\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100535 return -EINVAL;
536 }
537 if (pci_offset_low & (granularity - 1)) {
Martyn Welch48d93562010-03-22 14:58:50 +0000538 dev_err(tsi148_bridge->parent, "Invalid PCI Offset "
539 "alignment\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100540 return -EINVAL;
541 }
542
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100543 /* Disable while we are mucking around */
Martyn Welch29848ac2010-02-18 15:13:05 +0000544 temp_ctl = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100545 TSI148_LCSR_OFFSET_ITAT);
546 temp_ctl &= ~TSI148_LCSR_ITAT_EN;
Martyn Welch29848ac2010-02-18 15:13:05 +0000547 iowrite32be(temp_ctl, bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100548 TSI148_LCSR_OFFSET_ITAT);
549
550 /* Setup mapping */
Martyn Welch29848ac2010-02-18 15:13:05 +0000551 iowrite32be(vme_base_high, bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100552 TSI148_LCSR_OFFSET_ITSAU);
Martyn Welch29848ac2010-02-18 15:13:05 +0000553 iowrite32be(vme_base_low, bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100554 TSI148_LCSR_OFFSET_ITSAL);
Martyn Welch29848ac2010-02-18 15:13:05 +0000555 iowrite32be(vme_bound_high, bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100556 TSI148_LCSR_OFFSET_ITEAU);
Martyn Welch29848ac2010-02-18 15:13:05 +0000557 iowrite32be(vme_bound_low, bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100558 TSI148_LCSR_OFFSET_ITEAL);
Martyn Welch29848ac2010-02-18 15:13:05 +0000559 iowrite32be(pci_offset_high, bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100560 TSI148_LCSR_OFFSET_ITOFU);
Martyn Welch29848ac2010-02-18 15:13:05 +0000561 iowrite32be(pci_offset_low, bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100562 TSI148_LCSR_OFFSET_ITOFL);
563
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100564 /* Setup 2eSST speeds */
565 temp_ctl &= ~TSI148_LCSR_ITAT_2eSSTM_M;
566 switch (cycle & (VME_2eSST160 | VME_2eSST267 | VME_2eSST320)) {
567 case VME_2eSST160:
568 temp_ctl |= TSI148_LCSR_ITAT_2eSSTM_160;
569 break;
570 case VME_2eSST267:
571 temp_ctl |= TSI148_LCSR_ITAT_2eSSTM_267;
572 break;
573 case VME_2eSST320:
574 temp_ctl |= TSI148_LCSR_ITAT_2eSSTM_320;
575 break;
576 }
577
578 /* Setup cycle types */
579 temp_ctl &= ~(0x1F << 7);
580 if (cycle & VME_BLT)
581 temp_ctl |= TSI148_LCSR_ITAT_BLT;
582 if (cycle & VME_MBLT)
583 temp_ctl |= TSI148_LCSR_ITAT_MBLT;
584 if (cycle & VME_2eVME)
585 temp_ctl |= TSI148_LCSR_ITAT_2eVME;
586 if (cycle & VME_2eSST)
587 temp_ctl |= TSI148_LCSR_ITAT_2eSST;
588 if (cycle & VME_2eSSTB)
589 temp_ctl |= TSI148_LCSR_ITAT_2eSSTB;
590
591 /* Setup address space */
592 temp_ctl &= ~TSI148_LCSR_ITAT_AS_M;
593 temp_ctl |= addr;
594
595 temp_ctl &= ~0xF;
596 if (cycle & VME_SUPER)
597 temp_ctl |= TSI148_LCSR_ITAT_SUPR ;
598 if (cycle & VME_USER)
599 temp_ctl |= TSI148_LCSR_ITAT_NPRIV;
600 if (cycle & VME_PROG)
601 temp_ctl |= TSI148_LCSR_ITAT_PGM;
602 if (cycle & VME_DATA)
603 temp_ctl |= TSI148_LCSR_ITAT_DATA;
604
605 /* Write ctl reg without enable */
Martyn Welch29848ac2010-02-18 15:13:05 +0000606 iowrite32be(temp_ctl, bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100607 TSI148_LCSR_OFFSET_ITAT);
608
609 if (enabled)
610 temp_ctl |= TSI148_LCSR_ITAT_EN;
611
Martyn Welch29848ac2010-02-18 15:13:05 +0000612 iowrite32be(temp_ctl, bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100613 TSI148_LCSR_OFFSET_ITAT);
614
615 return 0;
616}
617
618/*
619 * Get slave window configuration.
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100620 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +0000621static int tsi148_slave_get(struct vme_slave_resource *image, int *enabled,
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100622 unsigned long long *vme_base, unsigned long long *size,
Martyn Welch6af04b02011-12-01 17:06:29 +0000623 dma_addr_t *pci_base, u32 *aspace, u32 *cycle)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100624{
625 unsigned int i, granularity = 0, ctl = 0;
626 unsigned int vme_base_low, vme_base_high;
627 unsigned int vme_bound_low, vme_bound_high;
628 unsigned int pci_offset_low, pci_offset_high;
629 unsigned long long vme_bound, pci_offset;
Martyn Welch29848ac2010-02-18 15:13:05 +0000630 struct tsi148_driver *bridge;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100631
Martyn Welch29848ac2010-02-18 15:13:05 +0000632 bridge = image->parent->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100633
634 i = image->number;
635
636 /* Read registers */
Martyn Welch29848ac2010-02-18 15:13:05 +0000637 ctl = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100638 TSI148_LCSR_OFFSET_ITAT);
639
Martyn Welch29848ac2010-02-18 15:13:05 +0000640 vme_base_high = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100641 TSI148_LCSR_OFFSET_ITSAU);
Martyn Welch29848ac2010-02-18 15:13:05 +0000642 vme_base_low = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100643 TSI148_LCSR_OFFSET_ITSAL);
Martyn Welch29848ac2010-02-18 15:13:05 +0000644 vme_bound_high = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100645 TSI148_LCSR_OFFSET_ITEAU);
Martyn Welch29848ac2010-02-18 15:13:05 +0000646 vme_bound_low = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100647 TSI148_LCSR_OFFSET_ITEAL);
Martyn Welch29848ac2010-02-18 15:13:05 +0000648 pci_offset_high = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100649 TSI148_LCSR_OFFSET_ITOFU);
Martyn Welch29848ac2010-02-18 15:13:05 +0000650 pci_offset_low = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100651 TSI148_LCSR_OFFSET_ITOFL);
652
653 /* Convert 64-bit variables to 2x 32-bit variables */
654 reg_join(vme_base_high, vme_base_low, vme_base);
655 reg_join(vme_bound_high, vme_bound_low, &vme_bound);
656 reg_join(pci_offset_high, pci_offset_low, &pci_offset);
657
Joe Schultz098ced82014-04-03 14:47:55 -0500658 *pci_base = (dma_addr_t)(*vme_base + pci_offset);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100659
660 *enabled = 0;
661 *aspace = 0;
662 *cycle = 0;
663
664 if (ctl & TSI148_LCSR_ITAT_EN)
665 *enabled = 1;
666
667 if ((ctl & TSI148_LCSR_ITAT_AS_M) == TSI148_LCSR_ITAT_AS_A16) {
668 granularity = 0x10;
669 *aspace |= VME_A16;
670 }
671 if ((ctl & TSI148_LCSR_ITAT_AS_M) == TSI148_LCSR_ITAT_AS_A24) {
672 granularity = 0x1000;
673 *aspace |= VME_A24;
674 }
675 if ((ctl & TSI148_LCSR_ITAT_AS_M) == TSI148_LCSR_ITAT_AS_A32) {
676 granularity = 0x10000;
677 *aspace |= VME_A32;
678 }
679 if ((ctl & TSI148_LCSR_ITAT_AS_M) == TSI148_LCSR_ITAT_AS_A64) {
680 granularity = 0x10000;
681 *aspace |= VME_A64;
682 }
683
684 /* Need granularity before we set the size */
685 *size = (unsigned long long)((vme_bound - *vme_base) + granularity);
686
687
688 if ((ctl & TSI148_LCSR_ITAT_2eSSTM_M) == TSI148_LCSR_ITAT_2eSSTM_160)
689 *cycle |= VME_2eSST160;
690 if ((ctl & TSI148_LCSR_ITAT_2eSSTM_M) == TSI148_LCSR_ITAT_2eSSTM_267)
691 *cycle |= VME_2eSST267;
692 if ((ctl & TSI148_LCSR_ITAT_2eSSTM_M) == TSI148_LCSR_ITAT_2eSSTM_320)
693 *cycle |= VME_2eSST320;
694
695 if (ctl & TSI148_LCSR_ITAT_BLT)
696 *cycle |= VME_BLT;
697 if (ctl & TSI148_LCSR_ITAT_MBLT)
698 *cycle |= VME_MBLT;
699 if (ctl & TSI148_LCSR_ITAT_2eVME)
700 *cycle |= VME_2eVME;
701 if (ctl & TSI148_LCSR_ITAT_2eSST)
702 *cycle |= VME_2eSST;
703 if (ctl & TSI148_LCSR_ITAT_2eSSTB)
704 *cycle |= VME_2eSSTB;
705
706 if (ctl & TSI148_LCSR_ITAT_SUPR)
707 *cycle |= VME_SUPER;
708 if (ctl & TSI148_LCSR_ITAT_NPRIV)
709 *cycle |= VME_USER;
710 if (ctl & TSI148_LCSR_ITAT_PGM)
711 *cycle |= VME_PROG;
712 if (ctl & TSI148_LCSR_ITAT_DATA)
713 *cycle |= VME_DATA;
714
715 return 0;
716}
717
718/*
719 * Allocate and map PCI Resource
720 */
721static int tsi148_alloc_resource(struct vme_master_resource *image,
722 unsigned long long size)
723{
724 unsigned long long existing_size;
725 int retval = 0;
726 struct pci_dev *pdev;
Martyn Welch29848ac2010-02-18 15:13:05 +0000727 struct vme_bridge *tsi148_bridge;
728
729 tsi148_bridge = image->parent;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100730
Aaron Sierra177581fa2014-04-03 14:48:27 -0500731 pdev = to_pci_dev(tsi148_bridge->parent);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100732
Martyn Welch8fafb472010-02-18 15:13:12 +0000733 existing_size = (unsigned long long)(image->bus_resource.end -
734 image->bus_resource.start);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100735
736 /* If the existing size is OK, return */
Martyn Welch59c22902009-10-29 16:35:01 +0000737 if ((size != 0) && (existing_size == (size - 1)))
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100738 return 0;
739
740 if (existing_size != 0) {
741 iounmap(image->kern_base);
742 image->kern_base = NULL;
Ilia Mirkin794a8942011-03-13 00:29:13 -0500743 kfree(image->bus_resource.name);
Emilio G. Cota886953e2010-11-12 11:14:07 +0000744 release_resource(&image->bus_resource);
745 memset(&image->bus_resource, 0, sizeof(struct resource));
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100746 }
747
Martyn Welch59c22902009-10-29 16:35:01 +0000748 /* Exit here if size is zero */
Martyn Welch79463282010-03-22 14:58:57 +0000749 if (size == 0)
Martyn Welch59c22902009-10-29 16:35:01 +0000750 return 0;
Martyn Welch59c22902009-10-29 16:35:01 +0000751
Martyn Welch8fafb472010-02-18 15:13:12 +0000752 if (image->bus_resource.name == NULL) {
Julia Lawall0aa3f132010-05-30 22:27:46 +0200753 image->bus_resource.name = kmalloc(VMENAMSIZ+3, GFP_ATOMIC);
Martyn Welch8fafb472010-02-18 15:13:12 +0000754 if (image->bus_resource.name == NULL) {
Martyn Welch48d93562010-03-22 14:58:50 +0000755 dev_err(tsi148_bridge->parent, "Unable to allocate "
756 "memory for resource name\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100757 retval = -ENOMEM;
758 goto err_name;
759 }
760 }
761
Martyn Welch8fafb472010-02-18 15:13:12 +0000762 sprintf((char *)image->bus_resource.name, "%s.%d", tsi148_bridge->name,
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100763 image->number);
764
Martyn Welch8fafb472010-02-18 15:13:12 +0000765 image->bus_resource.start = 0;
766 image->bus_resource.end = (unsigned long)size;
767 image->bus_resource.flags = IORESOURCE_MEM;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100768
769 retval = pci_bus_alloc_resource(pdev->bus,
Dmitry Kalinkinda5ae8a2015-07-08 17:42:17 +0300770 &image->bus_resource, size, 0x10000, PCIBIOS_MIN_MEM,
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100771 0, NULL, NULL);
772 if (retval) {
Martyn Welch48d93562010-03-22 14:58:50 +0000773 dev_err(tsi148_bridge->parent, "Failed to allocate mem "
774 "resource for window %d size 0x%lx start 0x%lx\n",
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100775 image->number, (unsigned long)size,
Martyn Welch8fafb472010-02-18 15:13:12 +0000776 (unsigned long)image->bus_resource.start);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100777 goto err_resource;
778 }
779
780 image->kern_base = ioremap_nocache(
Martyn Welch8fafb472010-02-18 15:13:12 +0000781 image->bus_resource.start, size);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100782 if (image->kern_base == NULL) {
Martyn Welch48d93562010-03-22 14:58:50 +0000783 dev_err(tsi148_bridge->parent, "Failed to remap resource\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100784 retval = -ENOMEM;
785 goto err_remap;
786 }
787
788 return 0;
789
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100790err_remap:
Emilio G. Cota886953e2010-11-12 11:14:07 +0000791 release_resource(&image->bus_resource);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100792err_resource:
Martyn Welch8fafb472010-02-18 15:13:12 +0000793 kfree(image->bus_resource.name);
Emilio G. Cota886953e2010-11-12 11:14:07 +0000794 memset(&image->bus_resource, 0, sizeof(struct resource));
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100795err_name:
796 return retval;
797}
798
799/*
800 * Free and unmap PCI Resource
801 */
802static void tsi148_free_resource(struct vme_master_resource *image)
803{
804 iounmap(image->kern_base);
805 image->kern_base = NULL;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000806 release_resource(&image->bus_resource);
Martyn Welch8fafb472010-02-18 15:13:12 +0000807 kfree(image->bus_resource.name);
Emilio G. Cota886953e2010-11-12 11:14:07 +0000808 memset(&image->bus_resource, 0, sizeof(struct resource));
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100809}
810
811/*
812 * Set the attributes of an outbound window.
813 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +0000814static int tsi148_master_set(struct vme_master_resource *image, int enabled,
Martyn Welch6af04b02011-12-01 17:06:29 +0000815 unsigned long long vme_base, unsigned long long size, u32 aspace,
816 u32 cycle, u32 dwidth)
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100817{
818 int retval = 0;
819 unsigned int i;
820 unsigned int temp_ctl = 0;
821 unsigned int pci_base_low, pci_base_high;
822 unsigned int pci_bound_low, pci_bound_high;
823 unsigned int vme_offset_low, vme_offset_high;
824 unsigned long long pci_bound, vme_offset, pci_base;
Martyn Welch48d93562010-03-22 14:58:50 +0000825 struct vme_bridge *tsi148_bridge;
Martyn Welch29848ac2010-02-18 15:13:05 +0000826 struct tsi148_driver *bridge;
Joe Schultz226572b2014-04-03 14:48:16 -0500827 struct pci_bus_region region;
828 struct pci_dev *pdev;
Martyn Welch29848ac2010-02-18 15:13:05 +0000829
Martyn Welch48d93562010-03-22 14:58:50 +0000830 tsi148_bridge = image->parent;
831
832 bridge = tsi148_bridge->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100833
Aaron Sierra177581fa2014-04-03 14:48:27 -0500834 pdev = to_pci_dev(tsi148_bridge->parent);
Joe Schultz226572b2014-04-03 14:48:16 -0500835
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100836 /* Verify input data */
837 if (vme_base & 0xFFFF) {
Martyn Welch48d93562010-03-22 14:58:50 +0000838 dev_err(tsi148_bridge->parent, "Invalid VME Window "
839 "alignment\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100840 retval = -EINVAL;
841 goto err_window;
842 }
Martyn Welch59c22902009-10-29 16:35:01 +0000843
844 if ((size == 0) && (enabled != 0)) {
Martyn Welch48d93562010-03-22 14:58:50 +0000845 dev_err(tsi148_bridge->parent, "Size must be non-zero for "
846 "enabled windows\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100847 retval = -EINVAL;
848 goto err_window;
849 }
850
Emilio G. Cota886953e2010-11-12 11:14:07 +0000851 spin_lock(&image->lock);
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100852
853 /* Let's allocate the resource here rather than further up the stack as
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300854 * it avoids pushing loads of bus dependent stuff up the stack. If size
Martyn Welch59c22902009-10-29 16:35:01 +0000855 * is zero, any existing resource will be freed.
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100856 */
857 retval = tsi148_alloc_resource(image, size);
858 if (retval) {
Emilio G. Cota886953e2010-11-12 11:14:07 +0000859 spin_unlock(&image->lock);
Martyn Welch48d93562010-03-22 14:58:50 +0000860 dev_err(tsi148_bridge->parent, "Unable to allocate memory for "
Martyn Welch59c22902009-10-29 16:35:01 +0000861 "resource\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100862 goto err_res;
863 }
864
Martyn Welch59c22902009-10-29 16:35:01 +0000865 if (size == 0) {
866 pci_base = 0;
867 pci_bound = 0;
868 vme_offset = 0;
869 } else {
Joe Schultz226572b2014-04-03 14:48:16 -0500870 pcibios_resource_to_bus(pdev->bus, &region,
871 &image->bus_resource);
872 pci_base = region.start;
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100873
Martyn Welch59c22902009-10-29 16:35:01 +0000874 /*
875 * Bound address is a valid address for the window, adjust
876 * according to window granularity.
877 */
878 pci_bound = pci_base + (size - 0x10000);
879 vme_offset = vme_base - pci_base;
880 }
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100881
882 /* Convert 64-bit variables to 2x 32-bit variables */
883 reg_split(pci_base, &pci_base_high, &pci_base_low);
884 reg_split(pci_bound, &pci_bound_high, &pci_bound_low);
885 reg_split(vme_offset, &vme_offset_high, &vme_offset_low);
886
887 if (pci_base_low & 0xFFFF) {
Emilio G. Cota886953e2010-11-12 11:14:07 +0000888 spin_unlock(&image->lock);
Martyn Welch48d93562010-03-22 14:58:50 +0000889 dev_err(tsi148_bridge->parent, "Invalid PCI base alignment\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100890 retval = -EINVAL;
891 goto err_gran;
892 }
893 if (pci_bound_low & 0xFFFF) {
Emilio G. Cota886953e2010-11-12 11:14:07 +0000894 spin_unlock(&image->lock);
Martyn Welch48d93562010-03-22 14:58:50 +0000895 dev_err(tsi148_bridge->parent, "Invalid PCI bound alignment\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100896 retval = -EINVAL;
897 goto err_gran;
898 }
899 if (vme_offset_low & 0xFFFF) {
Emilio G. Cota886953e2010-11-12 11:14:07 +0000900 spin_unlock(&image->lock);
Martyn Welch48d93562010-03-22 14:58:50 +0000901 dev_err(tsi148_bridge->parent, "Invalid VME Offset "
902 "alignment\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100903 retval = -EINVAL;
904 goto err_gran;
905 }
906
907 i = image->number;
908
909 /* Disable while we are mucking around */
Martyn Welch29848ac2010-02-18 15:13:05 +0000910 temp_ctl = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100911 TSI148_LCSR_OFFSET_OTAT);
912 temp_ctl &= ~TSI148_LCSR_OTAT_EN;
Martyn Welch29848ac2010-02-18 15:13:05 +0000913 iowrite32be(temp_ctl, bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100914 TSI148_LCSR_OFFSET_OTAT);
915
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100916 /* Setup 2eSST speeds */
917 temp_ctl &= ~TSI148_LCSR_OTAT_2eSSTM_M;
918 switch (cycle & (VME_2eSST160 | VME_2eSST267 | VME_2eSST320)) {
919 case VME_2eSST160:
920 temp_ctl |= TSI148_LCSR_OTAT_2eSSTM_160;
921 break;
922 case VME_2eSST267:
923 temp_ctl |= TSI148_LCSR_OTAT_2eSSTM_267;
924 break;
925 case VME_2eSST320:
926 temp_ctl |= TSI148_LCSR_OTAT_2eSSTM_320;
927 break;
928 }
929
930 /* Setup cycle types */
931 if (cycle & VME_BLT) {
932 temp_ctl &= ~TSI148_LCSR_OTAT_TM_M;
933 temp_ctl |= TSI148_LCSR_OTAT_TM_BLT;
934 }
935 if (cycle & VME_MBLT) {
936 temp_ctl &= ~TSI148_LCSR_OTAT_TM_M;
937 temp_ctl |= TSI148_LCSR_OTAT_TM_MBLT;
938 }
939 if (cycle & VME_2eVME) {
940 temp_ctl &= ~TSI148_LCSR_OTAT_TM_M;
941 temp_ctl |= TSI148_LCSR_OTAT_TM_2eVME;
942 }
943 if (cycle & VME_2eSST) {
944 temp_ctl &= ~TSI148_LCSR_OTAT_TM_M;
945 temp_ctl |= TSI148_LCSR_OTAT_TM_2eSST;
946 }
947 if (cycle & VME_2eSSTB) {
Martyn Welch48d93562010-03-22 14:58:50 +0000948 dev_warn(tsi148_bridge->parent, "Currently not setting "
949 "Broadcast Select Registers\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100950 temp_ctl &= ~TSI148_LCSR_OTAT_TM_M;
951 temp_ctl |= TSI148_LCSR_OTAT_TM_2eSSTB;
952 }
953
954 /* Setup data width */
955 temp_ctl &= ~TSI148_LCSR_OTAT_DBW_M;
956 switch (dwidth) {
957 case VME_D16:
958 temp_ctl |= TSI148_LCSR_OTAT_DBW_16;
959 break;
960 case VME_D32:
961 temp_ctl |= TSI148_LCSR_OTAT_DBW_32;
962 break;
963 default:
Emilio G. Cota886953e2010-11-12 11:14:07 +0000964 spin_unlock(&image->lock);
Martyn Welch48d93562010-03-22 14:58:50 +0000965 dev_err(tsi148_bridge->parent, "Invalid data width\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +0100966 retval = -EINVAL;
967 goto err_dwidth;
968 }
969
970 /* Setup address space */
971 temp_ctl &= ~TSI148_LCSR_OTAT_AMODE_M;
972 switch (aspace) {
973 case VME_A16:
974 temp_ctl |= TSI148_LCSR_OTAT_AMODE_A16;
975 break;
976 case VME_A24:
977 temp_ctl |= TSI148_LCSR_OTAT_AMODE_A24;
978 break;
979 case VME_A32:
980 temp_ctl |= TSI148_LCSR_OTAT_AMODE_A32;
981 break;
982 case VME_A64:
983 temp_ctl |= TSI148_LCSR_OTAT_AMODE_A64;
984 break;
985 case VME_CRCSR:
986 temp_ctl |= TSI148_LCSR_OTAT_AMODE_CRCSR;
987 break;
988 case VME_USER1:
989 temp_ctl |= TSI148_LCSR_OTAT_AMODE_USER1;
990 break;
991 case VME_USER2:
992 temp_ctl |= TSI148_LCSR_OTAT_AMODE_USER2;
993 break;
994 case VME_USER3:
995 temp_ctl |= TSI148_LCSR_OTAT_AMODE_USER3;
996 break;
997 case VME_USER4:
998 temp_ctl |= TSI148_LCSR_OTAT_AMODE_USER4;
999 break;
1000 default:
Emilio G. Cota886953e2010-11-12 11:14:07 +00001001 spin_unlock(&image->lock);
Martyn Welch48d93562010-03-22 14:58:50 +00001002 dev_err(tsi148_bridge->parent, "Invalid address space\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001003 retval = -EINVAL;
1004 goto err_aspace;
1005 break;
1006 }
1007
1008 temp_ctl &= ~(3<<4);
1009 if (cycle & VME_SUPER)
1010 temp_ctl |= TSI148_LCSR_OTAT_SUP;
1011 if (cycle & VME_PROG)
1012 temp_ctl |= TSI148_LCSR_OTAT_PGM;
1013
1014 /* Setup mapping */
Martyn Welch29848ac2010-02-18 15:13:05 +00001015 iowrite32be(pci_base_high, bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001016 TSI148_LCSR_OFFSET_OTSAU);
Martyn Welch29848ac2010-02-18 15:13:05 +00001017 iowrite32be(pci_base_low, bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001018 TSI148_LCSR_OFFSET_OTSAL);
Martyn Welch29848ac2010-02-18 15:13:05 +00001019 iowrite32be(pci_bound_high, bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001020 TSI148_LCSR_OFFSET_OTEAU);
Martyn Welch29848ac2010-02-18 15:13:05 +00001021 iowrite32be(pci_bound_low, bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001022 TSI148_LCSR_OFFSET_OTEAL);
Martyn Welch29848ac2010-02-18 15:13:05 +00001023 iowrite32be(vme_offset_high, bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001024 TSI148_LCSR_OFFSET_OTOFU);
Martyn Welch29848ac2010-02-18 15:13:05 +00001025 iowrite32be(vme_offset_low, bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001026 TSI148_LCSR_OFFSET_OTOFL);
1027
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001028 /* Write ctl reg without enable */
Martyn Welch29848ac2010-02-18 15:13:05 +00001029 iowrite32be(temp_ctl, bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001030 TSI148_LCSR_OFFSET_OTAT);
1031
1032 if (enabled)
1033 temp_ctl |= TSI148_LCSR_OTAT_EN;
1034
Martyn Welch29848ac2010-02-18 15:13:05 +00001035 iowrite32be(temp_ctl, bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001036 TSI148_LCSR_OFFSET_OTAT);
1037
Emilio G. Cota886953e2010-11-12 11:14:07 +00001038 spin_unlock(&image->lock);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001039 return 0;
1040
1041err_aspace:
1042err_dwidth:
1043err_gran:
1044 tsi148_free_resource(image);
1045err_res:
1046err_window:
1047 return retval;
1048
1049}
1050
1051/*
1052 * Set the attributes of an outbound window.
1053 *
1054 * XXX Not parsing prefetch information.
1055 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00001056static int __tsi148_master_get(struct vme_master_resource *image, int *enabled,
Martyn Welch6af04b02011-12-01 17:06:29 +00001057 unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
1058 u32 *cycle, u32 *dwidth)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001059{
1060 unsigned int i, ctl;
1061 unsigned int pci_base_low, pci_base_high;
1062 unsigned int pci_bound_low, pci_bound_high;
1063 unsigned int vme_offset_low, vme_offset_high;
1064
1065 unsigned long long pci_base, pci_bound, vme_offset;
Martyn Welch29848ac2010-02-18 15:13:05 +00001066 struct tsi148_driver *bridge;
1067
1068 bridge = image->parent->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001069
1070 i = image->number;
1071
Martyn Welch29848ac2010-02-18 15:13:05 +00001072 ctl = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001073 TSI148_LCSR_OFFSET_OTAT);
1074
Martyn Welch29848ac2010-02-18 15:13:05 +00001075 pci_base_high = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001076 TSI148_LCSR_OFFSET_OTSAU);
Martyn Welch29848ac2010-02-18 15:13:05 +00001077 pci_base_low = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001078 TSI148_LCSR_OFFSET_OTSAL);
Martyn Welch29848ac2010-02-18 15:13:05 +00001079 pci_bound_high = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001080 TSI148_LCSR_OFFSET_OTEAU);
Martyn Welch29848ac2010-02-18 15:13:05 +00001081 pci_bound_low = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001082 TSI148_LCSR_OFFSET_OTEAL);
Martyn Welch29848ac2010-02-18 15:13:05 +00001083 vme_offset_high = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001084 TSI148_LCSR_OFFSET_OTOFU);
Martyn Welch29848ac2010-02-18 15:13:05 +00001085 vme_offset_low = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001086 TSI148_LCSR_OFFSET_OTOFL);
1087
1088 /* Convert 64-bit variables to 2x 32-bit variables */
1089 reg_join(pci_base_high, pci_base_low, &pci_base);
1090 reg_join(pci_bound_high, pci_bound_low, &pci_bound);
1091 reg_join(vme_offset_high, vme_offset_low, &vme_offset);
1092
1093 *vme_base = pci_base + vme_offset;
1094 *size = (unsigned long long)(pci_bound - pci_base) + 0x10000;
1095
1096 *enabled = 0;
1097 *aspace = 0;
1098 *cycle = 0;
1099 *dwidth = 0;
1100
1101 if (ctl & TSI148_LCSR_OTAT_EN)
1102 *enabled = 1;
1103
1104 /* Setup address space */
1105 if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_A16)
1106 *aspace |= VME_A16;
1107 if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_A24)
1108 *aspace |= VME_A24;
1109 if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_A32)
1110 *aspace |= VME_A32;
1111 if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_A64)
1112 *aspace |= VME_A64;
1113 if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_CRCSR)
1114 *aspace |= VME_CRCSR;
1115 if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_USER1)
1116 *aspace |= VME_USER1;
1117 if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_USER2)
1118 *aspace |= VME_USER2;
1119 if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_USER3)
1120 *aspace |= VME_USER3;
1121 if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_USER4)
1122 *aspace |= VME_USER4;
1123
1124 /* Setup 2eSST speeds */
1125 if ((ctl & TSI148_LCSR_OTAT_2eSSTM_M) == TSI148_LCSR_OTAT_2eSSTM_160)
1126 *cycle |= VME_2eSST160;
1127 if ((ctl & TSI148_LCSR_OTAT_2eSSTM_M) == TSI148_LCSR_OTAT_2eSSTM_267)
1128 *cycle |= VME_2eSST267;
1129 if ((ctl & TSI148_LCSR_OTAT_2eSSTM_M) == TSI148_LCSR_OTAT_2eSSTM_320)
1130 *cycle |= VME_2eSST320;
1131
1132 /* Setup cycle types */
Martyn Welch79463282010-03-22 14:58:57 +00001133 if ((ctl & TSI148_LCSR_OTAT_TM_M) == TSI148_LCSR_OTAT_TM_SCT)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001134 *cycle |= VME_SCT;
Martyn Welch79463282010-03-22 14:58:57 +00001135 if ((ctl & TSI148_LCSR_OTAT_TM_M) == TSI148_LCSR_OTAT_TM_BLT)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001136 *cycle |= VME_BLT;
Martyn Welch79463282010-03-22 14:58:57 +00001137 if ((ctl & TSI148_LCSR_OTAT_TM_M) == TSI148_LCSR_OTAT_TM_MBLT)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001138 *cycle |= VME_MBLT;
Martyn Welch79463282010-03-22 14:58:57 +00001139 if ((ctl & TSI148_LCSR_OTAT_TM_M) == TSI148_LCSR_OTAT_TM_2eVME)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001140 *cycle |= VME_2eVME;
Martyn Welch79463282010-03-22 14:58:57 +00001141 if ((ctl & TSI148_LCSR_OTAT_TM_M) == TSI148_LCSR_OTAT_TM_2eSST)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001142 *cycle |= VME_2eSST;
Martyn Welch79463282010-03-22 14:58:57 +00001143 if ((ctl & TSI148_LCSR_OTAT_TM_M) == TSI148_LCSR_OTAT_TM_2eSSTB)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001144 *cycle |= VME_2eSSTB;
1145
1146 if (ctl & TSI148_LCSR_OTAT_SUP)
1147 *cycle |= VME_SUPER;
1148 else
1149 *cycle |= VME_USER;
1150
1151 if (ctl & TSI148_LCSR_OTAT_PGM)
1152 *cycle |= VME_PROG;
1153 else
1154 *cycle |= VME_DATA;
1155
1156 /* Setup data width */
1157 if ((ctl & TSI148_LCSR_OTAT_DBW_M) == TSI148_LCSR_OTAT_DBW_16)
1158 *dwidth = VME_D16;
1159 if ((ctl & TSI148_LCSR_OTAT_DBW_M) == TSI148_LCSR_OTAT_DBW_32)
1160 *dwidth = VME_D32;
1161
1162 return 0;
1163}
1164
1165
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00001166static int tsi148_master_get(struct vme_master_resource *image, int *enabled,
Martyn Welch6af04b02011-12-01 17:06:29 +00001167 unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
1168 u32 *cycle, u32 *dwidth)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001169{
1170 int retval;
1171
Emilio G. Cota886953e2010-11-12 11:14:07 +00001172 spin_lock(&image->lock);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001173
1174 retval = __tsi148_master_get(image, enabled, vme_base, size, aspace,
1175 cycle, dwidth);
1176
Emilio G. Cota886953e2010-11-12 11:14:07 +00001177 spin_unlock(&image->lock);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001178
1179 return retval;
1180}
1181
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00001182static ssize_t tsi148_master_read(struct vme_master_resource *image, void *buf,
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001183 size_t count, loff_t offset)
1184{
1185 int retval, enabled;
1186 unsigned long long vme_base, size;
Martyn Welch6af04b02011-12-01 17:06:29 +00001187 u32 aspace, cycle, dwidth;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001188 struct vme_bus_error *vme_err = NULL;
Martyn Welch29848ac2010-02-18 15:13:05 +00001189 struct vme_bridge *tsi148_bridge;
Jingoo Han4e8764d2013-08-19 16:40:15 +09001190 void __iomem *addr = image->kern_base + offset;
Martyn Welch363e2e62012-07-19 17:48:46 +01001191 unsigned int done = 0;
1192 unsigned int count32;
Martyn Welch29848ac2010-02-18 15:13:05 +00001193
1194 tsi148_bridge = image->parent;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001195
Emilio G. Cota886953e2010-11-12 11:14:07 +00001196 spin_lock(&image->lock);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001197
Martyn Welch363e2e62012-07-19 17:48:46 +01001198 /* The following code handles VME address alignment. We cannot use
Martyn Welcha2a720e2014-02-06 13:35:36 +00001199 * memcpy_xxx here because it may cut data transfers in to 8-bit
1200 * cycles when D16 or D32 cycles are required on the VME bus.
Martyn Welch363e2e62012-07-19 17:48:46 +01001201 * On the other hand, the bridge itself assures that the maximum data
1202 * cycle configured for the transfer is used and splits it
1203 * automatically for non-aligned addresses, so we don't want the
1204 * overhead of needlessly forcing small transfers for the entire cycle.
1205 */
1206 if ((uintptr_t)addr & 0x1) {
1207 *(u8 *)buf = ioread8(addr);
1208 done += 1;
1209 if (done == count)
1210 goto out;
1211 }
Martyn Welchf0342e62014-02-07 15:48:56 +00001212 if ((uintptr_t)(addr + done) & 0x2) {
Martyn Welch363e2e62012-07-19 17:48:46 +01001213 if ((count - done) < 2) {
1214 *(u8 *)(buf + done) = ioread8(addr + done);
1215 done += 1;
1216 goto out;
1217 } else {
1218 *(u16 *)(buf + done) = ioread16(addr + done);
1219 done += 2;
1220 }
1221 }
1222
1223 count32 = (count - done) & ~0x3;
Martyn Welcha2a720e2014-02-06 13:35:36 +00001224 while (done < count32) {
1225 *(u32 *)(buf + done) = ioread32(addr + done);
1226 done += 4;
Martyn Welch363e2e62012-07-19 17:48:46 +01001227 }
1228
1229 if ((count - done) & 0x2) {
1230 *(u16 *)(buf + done) = ioread16(addr + done);
1231 done += 2;
1232 }
1233 if ((count - done) & 0x1) {
1234 *(u8 *)(buf + done) = ioread8(addr + done);
1235 done += 1;
1236 }
1237
1238out:
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001239 retval = count;
1240
1241 if (!err_chk)
1242 goto skip_chk;
1243
1244 __tsi148_master_get(image, &enabled, &vme_base, &size, &aspace, &cycle,
1245 &dwidth);
1246
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001247 vme_err = vme_find_error(tsi148_bridge, aspace, vme_base + offset,
Martyn Welch29848ac2010-02-18 15:13:05 +00001248 count);
Martyn Welch79463282010-03-22 14:58:57 +00001249 if (vme_err != NULL) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001250 dev_err(image->parent->parent, "First VME read error detected "
1251 "an at address 0x%llx\n", vme_err->address);
1252 retval = vme_err->address - (vme_base + offset);
1253 /* Clear down save errors in this address range */
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001254 vme_clear_errors(tsi148_bridge, aspace, vme_base + offset,
Martyn Welch29848ac2010-02-18 15:13:05 +00001255 count);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001256 }
1257
1258skip_chk:
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
1275 struct vme_bus_error *vme_err = 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
Martyn Welch363e2e62012-07-19 17:48:46 +01001285 /* Here we apply for the same strategy we do in master_read
Martyn Welcha2a720e2014-02-06 13:35:36 +00001286 * function in order to assure the correct cycles.
Martyn Welch363e2e62012-07-19 17:48:46 +01001287 */
1288 if ((uintptr_t)addr & 0x1) {
1289 iowrite8(*(u8 *)buf, addr);
1290 done += 1;
1291 if (done == count)
1292 goto out;
1293 }
Martyn Welchf0342e62014-02-07 15:48:56 +00001294 if ((uintptr_t)(addr + done) & 0x2) {
Martyn Welch363e2e62012-07-19 17:48:46 +01001295 if ((count - done) < 2) {
1296 iowrite8(*(u8 *)(buf + done), addr + done);
1297 done += 1;
1298 goto out;
1299 } else {
1300 iowrite16(*(u16 *)(buf + done), addr + done);
1301 done += 2;
1302 }
1303 }
1304
1305 count32 = (count - done) & ~0x3;
Martyn Welcha2a720e2014-02-06 13:35:36 +00001306 while (done < count32) {
1307 iowrite32(*(u32 *)(buf + done), addr + done);
1308 done += 4;
Martyn Welch363e2e62012-07-19 17:48:46 +01001309 }
1310
1311 if ((count - done) & 0x2) {
1312 iowrite16(*(u16 *)(buf + done), addr + done);
1313 done += 2;
1314 }
1315 if ((count - done) & 0x1) {
1316 iowrite8(*(u8 *)(buf + done), addr + done);
1317 done += 1;
1318 }
1319
1320out:
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001321 retval = count;
1322
1323 /*
1324 * Writes are posted. We need to do a read on the VME bus to flush out
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001325 * all of the writes before we check for errors. We can't guarantee
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001326 * that reading the data we have just written is safe. It is believed
1327 * that there isn't any read, write re-ordering, so we can read any
1328 * location in VME space, so lets read the Device ID from the tsi148's
1329 * own registers as mapped into CR/CSR space.
1330 *
1331 * We check for saved errors in the written address range/space.
1332 */
1333
1334 if (!err_chk)
1335 goto skip_chk;
1336
1337 /*
1338 * Get window info first, to maximise the time that the buffers may
1339 * fluch on their own
1340 */
1341 __tsi148_master_get(image, &enabled, &vme_base, &size, &aspace, &cycle,
1342 &dwidth);
1343
Martyn Welch29848ac2010-02-18 15:13:05 +00001344 ioread16(bridge->flush_image->kern_base + 0x7F000);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001345
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001346 vme_err = vme_find_error(tsi148_bridge, aspace, vme_base + offset,
Martyn Welch29848ac2010-02-18 15:13:05 +00001347 count);
Martyn Welch79463282010-03-22 14:58:57 +00001348 if (vme_err != NULL) {
Martyn Welch48d93562010-03-22 14:58:50 +00001349 dev_warn(tsi148_bridge->parent, "First VME write error detected"
1350 " an at address 0x%llx\n", vme_err->address);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001351 retval = vme_err->address - (vme_base + offset);
1352 /* Clear down save errors in this address range */
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001353 vme_clear_errors(tsi148_bridge, aspace, vme_base + offset,
Martyn Welch29848ac2010-02-18 15:13:05 +00001354 count);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001355 }
1356
1357skip_chk:
Emilio G. Cota886953e2010-11-12 11:14:07 +00001358 spin_unlock(&image->lock);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001359
1360 return retval;
1361}
1362
1363/*
1364 * Perform an RMW cycle on the VME bus.
1365 *
1366 * Requires a previously configured master window, returns final value.
1367 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00001368static unsigned int tsi148_master_rmw(struct vme_master_resource *image,
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001369 unsigned int mask, unsigned int compare, unsigned int swap,
1370 loff_t offset)
1371{
1372 unsigned long long pci_addr;
1373 unsigned int pci_addr_high, pci_addr_low;
1374 u32 tmp, result;
1375 int i;
Martyn Welch29848ac2010-02-18 15:13:05 +00001376 struct tsi148_driver *bridge;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001377
Martyn Welch29848ac2010-02-18 15:13:05 +00001378 bridge = image->parent->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001379
1380 /* Find the PCI address that maps to the desired VME address */
1381 i = image->number;
1382
1383 /* Locking as we can only do one of these at a time */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001384 mutex_lock(&bridge->vme_rmw);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001385
1386 /* Lock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001387 spin_lock(&image->lock);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001388
Martyn Welch29848ac2010-02-18 15:13:05 +00001389 pci_addr_high = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001390 TSI148_LCSR_OFFSET_OTSAU);
Martyn Welch29848ac2010-02-18 15:13:05 +00001391 pci_addr_low = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001392 TSI148_LCSR_OFFSET_OTSAL);
1393
1394 reg_join(pci_addr_high, pci_addr_low, &pci_addr);
1395 reg_split(pci_addr + offset, &pci_addr_high, &pci_addr_low);
1396
1397 /* Configure registers */
Martyn Welch29848ac2010-02-18 15:13:05 +00001398 iowrite32be(mask, bridge->base + TSI148_LCSR_RMWEN);
1399 iowrite32be(compare, bridge->base + TSI148_LCSR_RMWC);
1400 iowrite32be(swap, bridge->base + TSI148_LCSR_RMWS);
1401 iowrite32be(pci_addr_high, bridge->base + TSI148_LCSR_RMWAU);
1402 iowrite32be(pci_addr_low, bridge->base + TSI148_LCSR_RMWAL);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001403
1404 /* Enable RMW */
Martyn Welch29848ac2010-02-18 15:13:05 +00001405 tmp = ioread32be(bridge->base + TSI148_LCSR_VMCTRL);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001406 tmp |= TSI148_LCSR_VMCTRL_RMWEN;
Martyn Welch29848ac2010-02-18 15:13:05 +00001407 iowrite32be(tmp, bridge->base + TSI148_LCSR_VMCTRL);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001408
1409 /* Kick process off with a read to the required address. */
1410 result = ioread32be(image->kern_base + offset);
1411
1412 /* Disable RMW */
Martyn Welch29848ac2010-02-18 15:13:05 +00001413 tmp = ioread32be(bridge->base + TSI148_LCSR_VMCTRL);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001414 tmp &= ~TSI148_LCSR_VMCTRL_RMWEN;
Martyn Welch29848ac2010-02-18 15:13:05 +00001415 iowrite32be(tmp, bridge->base + TSI148_LCSR_VMCTRL);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001416
Emilio G. Cota886953e2010-11-12 11:14:07 +00001417 spin_unlock(&image->lock);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001418
Emilio G. Cota886953e2010-11-12 11:14:07 +00001419 mutex_unlock(&bridge->vme_rmw);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001420
1421 return result;
1422}
1423
Martyn Welchac1a4f22012-03-22 13:27:30 +00001424static int tsi148_dma_set_vme_src_attributes(struct device *dev, __be32 *attr,
Martyn Welch6af04b02011-12-01 17:06:29 +00001425 u32 aspace, u32 cycle, u32 dwidth)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001426{
Martyn Welchac1a4f22012-03-22 13:27:30 +00001427 u32 val;
1428
1429 val = be32_to_cpu(*attr);
1430
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001431 /* Setup 2eSST speeds */
1432 switch (cycle & (VME_2eSST160 | VME_2eSST267 | VME_2eSST320)) {
1433 case VME_2eSST160:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001434 val |= TSI148_LCSR_DSAT_2eSSTM_160;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001435 break;
1436 case VME_2eSST267:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001437 val |= TSI148_LCSR_DSAT_2eSSTM_267;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001438 break;
1439 case VME_2eSST320:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001440 val |= TSI148_LCSR_DSAT_2eSSTM_320;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001441 break;
1442 }
1443
1444 /* Setup cycle types */
Martyn Welch79463282010-03-22 14:58:57 +00001445 if (cycle & VME_SCT)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001446 val |= TSI148_LCSR_DSAT_TM_SCT;
Martyn Welch79463282010-03-22 14:58:57 +00001447
1448 if (cycle & VME_BLT)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001449 val |= TSI148_LCSR_DSAT_TM_BLT;
Martyn Welch79463282010-03-22 14:58:57 +00001450
1451 if (cycle & VME_MBLT)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001452 val |= TSI148_LCSR_DSAT_TM_MBLT;
Martyn Welch79463282010-03-22 14:58:57 +00001453
1454 if (cycle & VME_2eVME)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001455 val |= TSI148_LCSR_DSAT_TM_2eVME;
Martyn Welch79463282010-03-22 14:58:57 +00001456
1457 if (cycle & VME_2eSST)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001458 val |= TSI148_LCSR_DSAT_TM_2eSST;
Martyn Welch79463282010-03-22 14:58:57 +00001459
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001460 if (cycle & VME_2eSSTB) {
Martyn Welch48d93562010-03-22 14:58:50 +00001461 dev_err(dev, "Currently not setting Broadcast Select "
1462 "Registers\n");
Martyn Welchac1a4f22012-03-22 13:27:30 +00001463 val |= TSI148_LCSR_DSAT_TM_2eSSTB;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001464 }
1465
1466 /* Setup data width */
1467 switch (dwidth) {
1468 case VME_D16:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001469 val |= TSI148_LCSR_DSAT_DBW_16;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001470 break;
1471 case VME_D32:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001472 val |= TSI148_LCSR_DSAT_DBW_32;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001473 break;
1474 default:
Martyn Welch48d93562010-03-22 14:58:50 +00001475 dev_err(dev, "Invalid data width\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001476 return -EINVAL;
1477 }
1478
1479 /* Setup address space */
1480 switch (aspace) {
1481 case VME_A16:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001482 val |= TSI148_LCSR_DSAT_AMODE_A16;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001483 break;
1484 case VME_A24:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001485 val |= TSI148_LCSR_DSAT_AMODE_A24;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001486 break;
1487 case VME_A32:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001488 val |= TSI148_LCSR_DSAT_AMODE_A32;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001489 break;
1490 case VME_A64:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001491 val |= TSI148_LCSR_DSAT_AMODE_A64;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001492 break;
1493 case VME_CRCSR:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001494 val |= TSI148_LCSR_DSAT_AMODE_CRCSR;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001495 break;
1496 case VME_USER1:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001497 val |= TSI148_LCSR_DSAT_AMODE_USER1;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001498 break;
1499 case VME_USER2:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001500 val |= TSI148_LCSR_DSAT_AMODE_USER2;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001501 break;
1502 case VME_USER3:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001503 val |= TSI148_LCSR_DSAT_AMODE_USER3;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001504 break;
1505 case VME_USER4:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001506 val |= TSI148_LCSR_DSAT_AMODE_USER4;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001507 break;
1508 default:
Martyn Welch48d93562010-03-22 14:58:50 +00001509 dev_err(dev, "Invalid address space\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001510 return -EINVAL;
1511 break;
1512 }
1513
1514 if (cycle & VME_SUPER)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001515 val |= TSI148_LCSR_DSAT_SUP;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001516 if (cycle & VME_PROG)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001517 val |= TSI148_LCSR_DSAT_PGM;
1518
1519 *attr = cpu_to_be32(val);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001520
1521 return 0;
1522}
1523
Martyn Welchac1a4f22012-03-22 13:27:30 +00001524static int tsi148_dma_set_vme_dest_attributes(struct device *dev, __be32 *attr,
Martyn Welch6af04b02011-12-01 17:06:29 +00001525 u32 aspace, u32 cycle, u32 dwidth)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001526{
Martyn Welchac1a4f22012-03-22 13:27:30 +00001527 u32 val;
1528
1529 val = be32_to_cpu(*attr);
1530
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001531 /* Setup 2eSST speeds */
1532 switch (cycle & (VME_2eSST160 | VME_2eSST267 | VME_2eSST320)) {
1533 case VME_2eSST160:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001534 val |= TSI148_LCSR_DDAT_2eSSTM_160;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001535 break;
1536 case VME_2eSST267:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001537 val |= TSI148_LCSR_DDAT_2eSSTM_267;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001538 break;
1539 case VME_2eSST320:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001540 val |= TSI148_LCSR_DDAT_2eSSTM_320;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001541 break;
1542 }
1543
1544 /* Setup cycle types */
Martyn Welch79463282010-03-22 14:58:57 +00001545 if (cycle & VME_SCT)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001546 val |= TSI148_LCSR_DDAT_TM_SCT;
Martyn Welch79463282010-03-22 14:58:57 +00001547
1548 if (cycle & VME_BLT)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001549 val |= TSI148_LCSR_DDAT_TM_BLT;
Martyn Welch79463282010-03-22 14:58:57 +00001550
1551 if (cycle & VME_MBLT)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001552 val |= TSI148_LCSR_DDAT_TM_MBLT;
Martyn Welch79463282010-03-22 14:58:57 +00001553
1554 if (cycle & VME_2eVME)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001555 val |= TSI148_LCSR_DDAT_TM_2eVME;
Martyn Welch79463282010-03-22 14:58:57 +00001556
1557 if (cycle & VME_2eSST)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001558 val |= TSI148_LCSR_DDAT_TM_2eSST;
Martyn Welch79463282010-03-22 14:58:57 +00001559
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001560 if (cycle & VME_2eSSTB) {
Martyn Welch48d93562010-03-22 14:58:50 +00001561 dev_err(dev, "Currently not setting Broadcast Select "
1562 "Registers\n");
Martyn Welchac1a4f22012-03-22 13:27:30 +00001563 val |= TSI148_LCSR_DDAT_TM_2eSSTB;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001564 }
1565
1566 /* Setup data width */
1567 switch (dwidth) {
1568 case VME_D16:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001569 val |= TSI148_LCSR_DDAT_DBW_16;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001570 break;
1571 case VME_D32:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001572 val |= TSI148_LCSR_DDAT_DBW_32;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001573 break;
1574 default:
Martyn Welch48d93562010-03-22 14:58:50 +00001575 dev_err(dev, "Invalid data width\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001576 return -EINVAL;
1577 }
1578
1579 /* Setup address space */
1580 switch (aspace) {
1581 case VME_A16:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001582 val |= TSI148_LCSR_DDAT_AMODE_A16;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001583 break;
1584 case VME_A24:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001585 val |= TSI148_LCSR_DDAT_AMODE_A24;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001586 break;
1587 case VME_A32:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001588 val |= TSI148_LCSR_DDAT_AMODE_A32;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001589 break;
1590 case VME_A64:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001591 val |= TSI148_LCSR_DDAT_AMODE_A64;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001592 break;
1593 case VME_CRCSR:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001594 val |= TSI148_LCSR_DDAT_AMODE_CRCSR;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001595 break;
1596 case VME_USER1:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001597 val |= TSI148_LCSR_DDAT_AMODE_USER1;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001598 break;
1599 case VME_USER2:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001600 val |= TSI148_LCSR_DDAT_AMODE_USER2;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001601 break;
1602 case VME_USER3:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001603 val |= TSI148_LCSR_DDAT_AMODE_USER3;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001604 break;
1605 case VME_USER4:
Martyn Welchac1a4f22012-03-22 13:27:30 +00001606 val |= TSI148_LCSR_DDAT_AMODE_USER4;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001607 break;
1608 default:
Martyn Welch48d93562010-03-22 14:58:50 +00001609 dev_err(dev, "Invalid address space\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001610 return -EINVAL;
1611 break;
1612 }
1613
1614 if (cycle & VME_SUPER)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001615 val |= TSI148_LCSR_DDAT_SUP;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001616 if (cycle & VME_PROG)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001617 val |= TSI148_LCSR_DDAT_PGM;
1618
1619 *attr = cpu_to_be32(val);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001620
1621 return 0;
1622}
1623
1624/*
1625 * Add a link list descriptor to the list
Martyn Welchac1a4f22012-03-22 13:27:30 +00001626 *
1627 * Note: DMA engine expects the DMA descriptor to be big endian.
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001628 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00001629static int tsi148_dma_list_add(struct vme_dma_list *list,
1630 struct vme_dma_attr *src, struct vme_dma_attr *dest, size_t count)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001631{
1632 struct tsi148_dma_entry *entry, *prev;
Martyn Welchac1a4f22012-03-22 13:27:30 +00001633 u32 address_high, address_low, val;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001634 struct vme_dma_pattern *pattern_attr;
1635 struct vme_dma_pci *pci_attr;
1636 struct vme_dma_vme *vme_attr;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001637 int retval = 0;
Martyn Welch48d93562010-03-22 14:58:50 +00001638 struct vme_bridge *tsi148_bridge;
1639
1640 tsi148_bridge = list->parent->parent;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001641
Martyn Welchbb9ea892010-02-18 16:22:13 +00001642 /* Descriptor must be aligned on 64-bit boundaries */
Martyn Welch79463282010-03-22 14:58:57 +00001643 entry = kmalloc(sizeof(struct tsi148_dma_entry), GFP_KERNEL);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001644 if (entry == NULL) {
Martyn Welch48d93562010-03-22 14:58:50 +00001645 dev_err(tsi148_bridge->parent, "Failed to allocate memory for "
1646 "dma resource structure\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001647 retval = -ENOMEM;
1648 goto err_mem;
1649 }
1650
1651 /* Test descriptor alignment */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001652 if ((unsigned long)&entry->descriptor & 0x7) {
Martyn Welch48d93562010-03-22 14:58:50 +00001653 dev_err(tsi148_bridge->parent, "Descriptor not aligned to 8 "
1654 "byte boundary as required: %p\n",
Emilio G. Cota886953e2010-11-12 11:14:07 +00001655 &entry->descriptor);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001656 retval = -EINVAL;
1657 goto err_align;
1658 }
1659
1660 /* Given we are going to fill out the structure, we probably don't
1661 * need to zero it, but better safe than sorry for now.
1662 */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001663 memset(&entry->descriptor, 0, sizeof(struct tsi148_dma_descriptor));
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001664
1665 /* Fill out source part */
1666 switch (src->type) {
1667 case VME_DMA_PATTERN:
Kulikov Vasiliyc4d82fb2010-06-29 14:16:20 +04001668 pattern_attr = src->private;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001669
Martyn Welchac1a4f22012-03-22 13:27:30 +00001670 entry->descriptor.dsal = cpu_to_be32(pattern_attr->pattern);
1671
1672 val = TSI148_LCSR_DSAT_TYP_PAT;
1673
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001674 /* Default behaviour is 32 bit pattern */
Martyn Welch79463282010-03-22 14:58:57 +00001675 if (pattern_attr->type & VME_DMA_PATTERN_BYTE)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001676 val |= TSI148_LCSR_DSAT_PSZ;
Martyn Welch79463282010-03-22 14:58:57 +00001677
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001678 /* It seems that the default behaviour is to increment */
Martyn Welch79463282010-03-22 14:58:57 +00001679 if ((pattern_attr->type & VME_DMA_PATTERN_INCREMENT) == 0)
Martyn Welchac1a4f22012-03-22 13:27:30 +00001680 val |= TSI148_LCSR_DSAT_NIN;
1681 entry->descriptor.dsat = cpu_to_be32(val);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001682 break;
1683 case VME_DMA_PCI:
Kulikov Vasiliyc4d82fb2010-06-29 14:16:20 +04001684 pci_attr = src->private;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001685
1686 reg_split((unsigned long long)pci_attr->address, &address_high,
1687 &address_low);
Martyn Welchac1a4f22012-03-22 13:27:30 +00001688 entry->descriptor.dsau = cpu_to_be32(address_high);
1689 entry->descriptor.dsal = cpu_to_be32(address_low);
1690 entry->descriptor.dsat = cpu_to_be32(TSI148_LCSR_DSAT_TYP_PCI);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001691 break;
1692 case VME_DMA_VME:
Kulikov Vasiliyc4d82fb2010-06-29 14:16:20 +04001693 vme_attr = src->private;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001694
1695 reg_split((unsigned long long)vme_attr->address, &address_high,
1696 &address_low);
Martyn Welchac1a4f22012-03-22 13:27:30 +00001697 entry->descriptor.dsau = cpu_to_be32(address_high);
1698 entry->descriptor.dsal = cpu_to_be32(address_low);
1699 entry->descriptor.dsat = cpu_to_be32(TSI148_LCSR_DSAT_TYP_VME);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001700
1701 retval = tsi148_dma_set_vme_src_attributes(
Emilio G. Cota886953e2010-11-12 11:14:07 +00001702 tsi148_bridge->parent, &entry->descriptor.dsat,
Martyn Welch48d93562010-03-22 14:58:50 +00001703 vme_attr->aspace, vme_attr->cycle, vme_attr->dwidth);
Martyn Welch79463282010-03-22 14:58:57 +00001704 if (retval < 0)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001705 goto err_source;
1706 break;
1707 default:
Martyn Welch48d93562010-03-22 14:58:50 +00001708 dev_err(tsi148_bridge->parent, "Invalid source type\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001709 retval = -EINVAL;
1710 goto err_source;
1711 break;
1712 }
1713
1714 /* Assume last link - this will be over-written by adding another */
Martyn Welchac1a4f22012-03-22 13:27:30 +00001715 entry->descriptor.dnlau = cpu_to_be32(0);
1716 entry->descriptor.dnlal = cpu_to_be32(TSI148_LCSR_DNLAL_LLA);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001717
1718 /* Fill out destination part */
1719 switch (dest->type) {
1720 case VME_DMA_PCI:
Kulikov Vasiliyc4d82fb2010-06-29 14:16:20 +04001721 pci_attr = dest->private;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001722
1723 reg_split((unsigned long long)pci_attr->address, &address_high,
1724 &address_low);
Martyn Welchac1a4f22012-03-22 13:27:30 +00001725 entry->descriptor.ddau = cpu_to_be32(address_high);
1726 entry->descriptor.ddal = cpu_to_be32(address_low);
1727 entry->descriptor.ddat = cpu_to_be32(TSI148_LCSR_DDAT_TYP_PCI);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001728 break;
1729 case VME_DMA_VME:
Kulikov Vasiliyc4d82fb2010-06-29 14:16:20 +04001730 vme_attr = dest->private;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001731
1732 reg_split((unsigned long long)vme_attr->address, &address_high,
1733 &address_low);
Martyn Welchac1a4f22012-03-22 13:27:30 +00001734 entry->descriptor.ddau = cpu_to_be32(address_high);
1735 entry->descriptor.ddal = cpu_to_be32(address_low);
1736 entry->descriptor.ddat = cpu_to_be32(TSI148_LCSR_DDAT_TYP_VME);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001737
1738 retval = tsi148_dma_set_vme_dest_attributes(
Emilio G. Cota886953e2010-11-12 11:14:07 +00001739 tsi148_bridge->parent, &entry->descriptor.ddat,
Martyn Welch48d93562010-03-22 14:58:50 +00001740 vme_attr->aspace, vme_attr->cycle, vme_attr->dwidth);
Martyn Welch79463282010-03-22 14:58:57 +00001741 if (retval < 0)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001742 goto err_dest;
1743 break;
1744 default:
Martyn Welch48d93562010-03-22 14:58:50 +00001745 dev_err(tsi148_bridge->parent, "Invalid destination type\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001746 retval = -EINVAL;
1747 goto err_dest;
1748 break;
1749 }
1750
1751 /* Fill out count */
Martyn Welchac1a4f22012-03-22 13:27:30 +00001752 entry->descriptor.dcnt = cpu_to_be32((u32)count);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001753
1754 /* Add to list */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001755 list_add_tail(&entry->list, &list->entries);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001756
Dmitry Kalinkinb2383c92015-05-28 15:07:00 +03001757 entry->dma_handle = dma_map_single(tsi148_bridge->parent,
1758 &entry->descriptor,
1759 sizeof(struct tsi148_dma_descriptor), DMA_TO_DEVICE);
1760 if (dma_mapping_error(tsi148_bridge->parent, entry->dma_handle)) {
1761 dev_err(tsi148_bridge->parent, "DMA mapping error\n");
1762 retval = -EINVAL;
1763 goto err_dma;
1764 }
1765
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001766 /* Fill out previous descriptors "Next Address" */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001767 if (entry->list.prev != &list->entries) {
Martyn Welchac1a4f22012-03-22 13:27:30 +00001768 reg_split((unsigned long long)entry->dma_handle, &address_high,
1769 &address_low);
Dmitry Kalinkinb2383c92015-05-28 15:07:00 +03001770 prev = list_entry(entry->list.prev, struct tsi148_dma_entry,
1771 list);
Dmitry Kalinkinf656eaee2015-05-28 15:06:59 +03001772 prev->descriptor.dnlau = cpu_to_be32(address_high);
1773 prev->descriptor.dnlal = cpu_to_be32(address_low);
Martyn Welchac1a4f22012-03-22 13:27:30 +00001774
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001775 }
1776
1777 return 0;
1778
Dmitry Kalinkinb2383c92015-05-28 15:07:00 +03001779err_dma:
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001780err_dest:
1781err_source:
1782err_align:
1783 kfree(entry);
1784err_mem:
1785 return retval;
1786}
1787
1788/*
1789 * Check to see if the provided DMA channel is busy.
1790 */
Martyn Welch29848ac2010-02-18 15:13:05 +00001791static int tsi148_dma_busy(struct vme_bridge *tsi148_bridge, int channel)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001792{
1793 u32 tmp;
Martyn Welch29848ac2010-02-18 15:13:05 +00001794 struct tsi148_driver *bridge;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001795
Martyn Welch29848ac2010-02-18 15:13:05 +00001796 bridge = tsi148_bridge->driver_priv;
1797
1798 tmp = ioread32be(bridge->base + TSI148_LCSR_DMA[channel] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001799 TSI148_LCSR_OFFSET_DSTA);
1800
1801 if (tmp & TSI148_LCSR_DSTA_BSY)
1802 return 0;
1803 else
1804 return 1;
1805
1806}
1807
1808/*
1809 * Execute a previously generated link list
1810 *
1811 * XXX Need to provide control register configuration.
1812 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00001813static int tsi148_dma_list_exec(struct vme_dma_list *list)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001814{
1815 struct vme_dma_resource *ctrlr;
Dmitry Kalinkin75c66b62015-05-28 15:07:01 +03001816 int channel, retval;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001817 struct tsi148_dma_entry *entry;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001818 u32 bus_addr_high, bus_addr_low;
1819 u32 val, dctlreg = 0;
Martyn Welch48d93562010-03-22 14:58:50 +00001820 struct vme_bridge *tsi148_bridge;
Martyn Welch29848ac2010-02-18 15:13:05 +00001821 struct tsi148_driver *bridge;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001822
1823 ctrlr = list->parent;
1824
Martyn Welch48d93562010-03-22 14:58:50 +00001825 tsi148_bridge = ctrlr->parent;
1826
1827 bridge = tsi148_bridge->driver_priv;
Martyn Welch29848ac2010-02-18 15:13:05 +00001828
Emilio G. Cota886953e2010-11-12 11:14:07 +00001829 mutex_lock(&ctrlr->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001830
1831 channel = ctrlr->number;
1832
Emilio G. Cota886953e2010-11-12 11:14:07 +00001833 if (!list_empty(&ctrlr->running)) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001834 /*
1835 * XXX We have an active DMA transfer and currently haven't
1836 * sorted out the mechanism for "pending" DMA transfers.
1837 * Return busy.
1838 */
1839 /* Need to add to pending here */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001840 mutex_unlock(&ctrlr->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001841 return -EBUSY;
1842 } else {
Emilio G. Cota886953e2010-11-12 11:14:07 +00001843 list_add(&list->list, &ctrlr->running);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001844 }
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001845
1846 /* Get first bus address and write into registers */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001847 entry = list_first_entry(&list->entries, struct tsi148_dma_entry,
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001848 list);
1849
Emilio G. Cota886953e2010-11-12 11:14:07 +00001850 mutex_unlock(&ctrlr->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001851
Martyn Welch3abc48a2012-03-22 13:27:29 +00001852 reg_split(entry->dma_handle, &bus_addr_high, &bus_addr_low);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001853
Martyn Welch29848ac2010-02-18 15:13:05 +00001854 iowrite32be(bus_addr_high, bridge->base +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001855 TSI148_LCSR_DMA[channel] + TSI148_LCSR_OFFSET_DNLAU);
Martyn Welch29848ac2010-02-18 15:13:05 +00001856 iowrite32be(bus_addr_low, bridge->base +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001857 TSI148_LCSR_DMA[channel] + TSI148_LCSR_OFFSET_DNLAL);
1858
Martyn Welchac1a4f22012-03-22 13:27:30 +00001859 dctlreg = ioread32be(bridge->base + TSI148_LCSR_DMA[channel] +
1860 TSI148_LCSR_OFFSET_DCTL);
1861
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001862 /* Start the operation */
Martyn Welch29848ac2010-02-18 15:13:05 +00001863 iowrite32be(dctlreg | TSI148_LCSR_DCTL_DGO, bridge->base +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001864 TSI148_LCSR_DMA[channel] + TSI148_LCSR_OFFSET_DCTL);
1865
Dmitry Kalinkin75c66b62015-05-28 15:07:01 +03001866 retval = wait_event_interruptible(bridge->dma_queue[channel],
Martyn Welch29848ac2010-02-18 15:13:05 +00001867 tsi148_dma_busy(ctrlr->parent, channel));
Martyn Welchac1a4f22012-03-22 13:27:30 +00001868
Dmitry Kalinkin75c66b62015-05-28 15:07:01 +03001869 if (retval) {
1870 iowrite32be(dctlreg | TSI148_LCSR_DCTL_ABT, bridge->base +
1871 TSI148_LCSR_DMA[channel] + TSI148_LCSR_OFFSET_DCTL);
1872 /* Wait for the operation to abort */
1873 wait_event(bridge->dma_queue[channel],
1874 tsi148_dma_busy(ctrlr->parent, channel));
1875 retval = -EINTR;
1876 goto exit;
1877 }
1878
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001879 /*
1880 * Read status register, this register is valid until we kick off a
1881 * new transfer.
1882 */
Martyn Welch29848ac2010-02-18 15:13:05 +00001883 val = ioread32be(bridge->base + TSI148_LCSR_DMA[channel] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001884 TSI148_LCSR_OFFSET_DSTA);
1885
1886 if (val & TSI148_LCSR_DSTA_VBE) {
Martyn Welch48d93562010-03-22 14:58:50 +00001887 dev_err(tsi148_bridge->parent, "DMA Error. DSTA=%08X\n", val);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001888 retval = -EIO;
1889 }
1890
Dmitry Kalinkin75c66b62015-05-28 15:07:01 +03001891exit:
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001892 /* Remove list from running list */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001893 mutex_lock(&ctrlr->mtx);
1894 list_del(&list->list);
1895 mutex_unlock(&ctrlr->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001896
1897 return retval;
1898}
1899
1900/*
1901 * Clean up a previously generated link list
1902 *
1903 * We have a separate function, don't assume that the chain can't be reused.
1904 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00001905static int tsi148_dma_list_empty(struct vme_dma_list *list)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001906{
1907 struct list_head *pos, *temp;
Martyn Welch79463282010-03-22 14:58:57 +00001908 struct tsi148_dma_entry *entry;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001909
Martyn Welch3abc48a2012-03-22 13:27:29 +00001910 struct vme_bridge *tsi148_bridge = list->parent->parent;
1911
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001912 /* detach and free each entry */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001913 list_for_each_safe(pos, temp, &list->entries) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001914 list_del(pos);
1915 entry = list_entry(pos, struct tsi148_dma_entry, list);
Martyn Welch3abc48a2012-03-22 13:27:29 +00001916
1917 dma_unmap_single(tsi148_bridge->parent, entry->dma_handle,
1918 sizeof(struct tsi148_dma_descriptor), DMA_TO_DEVICE);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001919 kfree(entry);
1920 }
1921
Martyn Welch79463282010-03-22 14:58:57 +00001922 return 0;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001923}
1924
1925/*
1926 * All 4 location monitors reside at the same base - this is therefore a
1927 * system wide configuration.
1928 *
1929 * This does not enable the LM monitor - that should be done when the first
1930 * callback is attached and disabled when the last callback is removed.
1931 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00001932static int tsi148_lm_set(struct vme_lm_resource *lm, unsigned long long lm_base,
Martyn Welch6af04b02011-12-01 17:06:29 +00001933 u32 aspace, u32 cycle)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001934{
1935 u32 lm_base_high, lm_base_low, lm_ctl = 0;
1936 int i;
Martyn Welch48d93562010-03-22 14:58:50 +00001937 struct vme_bridge *tsi148_bridge;
Martyn Welch29848ac2010-02-18 15:13:05 +00001938 struct tsi148_driver *bridge;
1939
Martyn Welch48d93562010-03-22 14:58:50 +00001940 tsi148_bridge = lm->parent;
1941
1942 bridge = tsi148_bridge->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001943
Emilio G. Cota886953e2010-11-12 11:14:07 +00001944 mutex_lock(&lm->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001945
1946 /* If we already have a callback attached, we can't move it! */
Martyn Welch42fb5032009-08-11 17:44:56 +01001947 for (i = 0; i < lm->monitors; i++) {
Martyn Welch29848ac2010-02-18 15:13:05 +00001948 if (bridge->lm_callback[i] != NULL) {
Emilio G. Cota886953e2010-11-12 11:14:07 +00001949 mutex_unlock(&lm->mtx);
Martyn Welch48d93562010-03-22 14:58:50 +00001950 dev_err(tsi148_bridge->parent, "Location monitor "
1951 "callback attached, can't reset\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001952 return -EBUSY;
1953 }
1954 }
1955
1956 switch (aspace) {
1957 case VME_A16:
1958 lm_ctl |= TSI148_LCSR_LMAT_AS_A16;
1959 break;
1960 case VME_A24:
1961 lm_ctl |= TSI148_LCSR_LMAT_AS_A24;
1962 break;
1963 case VME_A32:
1964 lm_ctl |= TSI148_LCSR_LMAT_AS_A32;
1965 break;
1966 case VME_A64:
1967 lm_ctl |= TSI148_LCSR_LMAT_AS_A64;
1968 break;
1969 default:
Emilio G. Cota886953e2010-11-12 11:14:07 +00001970 mutex_unlock(&lm->mtx);
Martyn Welch48d93562010-03-22 14:58:50 +00001971 dev_err(tsi148_bridge->parent, "Invalid address space\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001972 return -EINVAL;
1973 break;
1974 }
1975
1976 if (cycle & VME_SUPER)
1977 lm_ctl |= TSI148_LCSR_LMAT_SUPR ;
1978 if (cycle & VME_USER)
1979 lm_ctl |= TSI148_LCSR_LMAT_NPRIV;
1980 if (cycle & VME_PROG)
1981 lm_ctl |= TSI148_LCSR_LMAT_PGM;
1982 if (cycle & VME_DATA)
1983 lm_ctl |= TSI148_LCSR_LMAT_DATA;
1984
1985 reg_split(lm_base, &lm_base_high, &lm_base_low);
1986
Martyn Welch29848ac2010-02-18 15:13:05 +00001987 iowrite32be(lm_base_high, bridge->base + TSI148_LCSR_LMBAU);
1988 iowrite32be(lm_base_low, bridge->base + TSI148_LCSR_LMBAL);
1989 iowrite32be(lm_ctl, bridge->base + TSI148_LCSR_LMAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001990
Emilio G. Cota886953e2010-11-12 11:14:07 +00001991 mutex_unlock(&lm->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01001992
1993 return 0;
1994}
1995
1996/* Get configuration of the callback monitor and return whether it is enabled
1997 * or disabled.
1998 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00001999static int tsi148_lm_get(struct vme_lm_resource *lm,
Martyn Welch6af04b02011-12-01 17:06:29 +00002000 unsigned long long *lm_base, u32 *aspace, u32 *cycle)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002001{
2002 u32 lm_base_high, lm_base_low, lm_ctl, enabled = 0;
Martyn Welch29848ac2010-02-18 15:13:05 +00002003 struct tsi148_driver *bridge;
2004
2005 bridge = lm->parent->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002006
Emilio G. Cota886953e2010-11-12 11:14:07 +00002007 mutex_lock(&lm->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002008
Martyn Welch29848ac2010-02-18 15:13:05 +00002009 lm_base_high = ioread32be(bridge->base + TSI148_LCSR_LMBAU);
2010 lm_base_low = ioread32be(bridge->base + TSI148_LCSR_LMBAL);
2011 lm_ctl = ioread32be(bridge->base + TSI148_LCSR_LMAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002012
2013 reg_join(lm_base_high, lm_base_low, lm_base);
2014
2015 if (lm_ctl & TSI148_LCSR_LMAT_EN)
2016 enabled = 1;
2017
Martyn Welch79463282010-03-22 14:58:57 +00002018 if ((lm_ctl & TSI148_LCSR_LMAT_AS_M) == TSI148_LCSR_LMAT_AS_A16)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002019 *aspace |= VME_A16;
Martyn Welch79463282010-03-22 14:58:57 +00002020
2021 if ((lm_ctl & TSI148_LCSR_LMAT_AS_M) == TSI148_LCSR_LMAT_AS_A24)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002022 *aspace |= VME_A24;
Martyn Welch79463282010-03-22 14:58:57 +00002023
2024 if ((lm_ctl & TSI148_LCSR_LMAT_AS_M) == TSI148_LCSR_LMAT_AS_A32)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002025 *aspace |= VME_A32;
Martyn Welch79463282010-03-22 14:58:57 +00002026
2027 if ((lm_ctl & TSI148_LCSR_LMAT_AS_M) == TSI148_LCSR_LMAT_AS_A64)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002028 *aspace |= VME_A64;
Martyn Welch79463282010-03-22 14:58:57 +00002029
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002030
2031 if (lm_ctl & TSI148_LCSR_LMAT_SUPR)
2032 *cycle |= VME_SUPER;
2033 if (lm_ctl & TSI148_LCSR_LMAT_NPRIV)
2034 *cycle |= VME_USER;
2035 if (lm_ctl & TSI148_LCSR_LMAT_PGM)
2036 *cycle |= VME_PROG;
2037 if (lm_ctl & TSI148_LCSR_LMAT_DATA)
2038 *cycle |= VME_DATA;
2039
Emilio G. Cota886953e2010-11-12 11:14:07 +00002040 mutex_unlock(&lm->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002041
2042 return enabled;
2043}
2044
2045/*
2046 * Attach a callback to a specific location monitor.
2047 *
2048 * Callback will be passed the monitor triggered.
2049 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00002050static int tsi148_lm_attach(struct vme_lm_resource *lm, int monitor,
Martyn Welch42fb5032009-08-11 17:44:56 +01002051 void (*callback)(int))
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002052{
2053 u32 lm_ctl, tmp;
Martyn Welch48d93562010-03-22 14:58:50 +00002054 struct vme_bridge *tsi148_bridge;
Martyn Welch29848ac2010-02-18 15:13:05 +00002055 struct tsi148_driver *bridge;
2056
Martyn Welch48d93562010-03-22 14:58:50 +00002057 tsi148_bridge = lm->parent;
2058
2059 bridge = tsi148_bridge->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002060
Emilio G. Cota886953e2010-11-12 11:14:07 +00002061 mutex_lock(&lm->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002062
2063 /* Ensure that the location monitor is configured - need PGM or DATA */
Martyn Welch29848ac2010-02-18 15:13:05 +00002064 lm_ctl = ioread32be(bridge->base + TSI148_LCSR_LMAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002065 if ((lm_ctl & (TSI148_LCSR_LMAT_PGM | TSI148_LCSR_LMAT_DATA)) == 0) {
Emilio G. Cota886953e2010-11-12 11:14:07 +00002066 mutex_unlock(&lm->mtx);
Martyn Welch48d93562010-03-22 14:58:50 +00002067 dev_err(tsi148_bridge->parent, "Location monitor not properly "
2068 "configured\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002069 return -EINVAL;
2070 }
2071
2072 /* Check that a callback isn't already attached */
Martyn Welch29848ac2010-02-18 15:13:05 +00002073 if (bridge->lm_callback[monitor] != NULL) {
Emilio G. Cota886953e2010-11-12 11:14:07 +00002074 mutex_unlock(&lm->mtx);
Martyn Welch48d93562010-03-22 14:58:50 +00002075 dev_err(tsi148_bridge->parent, "Existing callback attached\n");
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002076 return -EBUSY;
2077 }
2078
2079 /* Attach callback */
Martyn Welch29848ac2010-02-18 15:13:05 +00002080 bridge->lm_callback[monitor] = callback;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002081
2082 /* Enable Location Monitor interrupt */
Martyn Welch29848ac2010-02-18 15:13:05 +00002083 tmp = ioread32be(bridge->base + TSI148_LCSR_INTEN);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002084 tmp |= TSI148_LCSR_INTEN_LMEN[monitor];
Martyn Welch29848ac2010-02-18 15:13:05 +00002085 iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEN);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002086
Martyn Welch29848ac2010-02-18 15:13:05 +00002087 tmp = ioread32be(bridge->base + TSI148_LCSR_INTEO);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002088 tmp |= TSI148_LCSR_INTEO_LMEO[monitor];
Martyn Welch29848ac2010-02-18 15:13:05 +00002089 iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEO);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002090
2091 /* Ensure that global Location Monitor Enable set */
2092 if ((lm_ctl & TSI148_LCSR_LMAT_EN) == 0) {
2093 lm_ctl |= TSI148_LCSR_LMAT_EN;
Martyn Welch29848ac2010-02-18 15:13:05 +00002094 iowrite32be(lm_ctl, bridge->base + TSI148_LCSR_LMAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002095 }
2096
Emilio G. Cota886953e2010-11-12 11:14:07 +00002097 mutex_unlock(&lm->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002098
2099 return 0;
2100}
2101
2102/*
2103 * Detach a callback function forn a specific location monitor.
2104 */
Emilio G. Cota5ade6c42010-11-12 11:15:00 +00002105static int tsi148_lm_detach(struct vme_lm_resource *lm, int monitor)
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002106{
2107 u32 lm_en, tmp;
Martyn Welch29848ac2010-02-18 15:13:05 +00002108 struct tsi148_driver *bridge;
2109
2110 bridge = lm->parent->driver_priv;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002111
Emilio G. Cota886953e2010-11-12 11:14:07 +00002112 mutex_lock(&lm->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002113
2114 /* Disable Location Monitor and ensure previous interrupts are clear */
Martyn Welch29848ac2010-02-18 15:13:05 +00002115 lm_en = ioread32be(bridge->base + TSI148_LCSR_INTEN);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002116 lm_en &= ~TSI148_LCSR_INTEN_LMEN[monitor];
Martyn Welch29848ac2010-02-18 15:13:05 +00002117 iowrite32be(lm_en, bridge->base + TSI148_LCSR_INTEN);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002118
Martyn Welch29848ac2010-02-18 15:13:05 +00002119 tmp = ioread32be(bridge->base + TSI148_LCSR_INTEO);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002120 tmp &= ~TSI148_LCSR_INTEO_LMEO[monitor];
Martyn Welch29848ac2010-02-18 15:13:05 +00002121 iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEO);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002122
2123 iowrite32be(TSI148_LCSR_INTC_LMC[monitor],
Martyn Welch29848ac2010-02-18 15:13:05 +00002124 bridge->base + TSI148_LCSR_INTC);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002125
2126 /* Detach callback */
Martyn Welch29848ac2010-02-18 15:13:05 +00002127 bridge->lm_callback[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);
Martyn Welch29848ac2010-02-18 15:13:05 +00002208 if (bridge->crcsr_kernel == NULL) {
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 */
Julia Lawall7a6cb0d2010-05-13 22:00:05 +02002294 tsi148_bridge = kzalloc(sizeof(struct vme_bridge), GFP_KERNEL);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002295 if (tsi148_bridge == NULL) {
2296 dev_err(&pdev->dev, "Failed to allocate memory for device "
2297 "structure\n");
2298 retval = -ENOMEM;
2299 goto err_struct;
2300 }
2301
Julia Lawall7a6cb0d2010-05-13 22:00:05 +02002302 tsi148_device = kzalloc(sizeof(struct tsi148_driver), GFP_KERNEL);
Martyn Welch29848ac2010-02-18 15:13:05 +00002303 if (tsi148_device == NULL) {
2304 dev_err(&pdev->dev, "Failed to allocate memory for device "
2305 "structure\n");
2306 retval = -ENOMEM;
2307 goto err_driver;
2308 }
2309
Martyn Welch29848ac2010-02-18 15:13:05 +00002310 tsi148_bridge->driver_priv = tsi148_device;
2311
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002312 /* Enable the device */
2313 retval = pci_enable_device(pdev);
2314 if (retval) {
2315 dev_err(&pdev->dev, "Unable to enable device\n");
2316 goto err_enable;
2317 }
2318
2319 /* Map Registers */
2320 retval = pci_request_regions(pdev, driver_name);
2321 if (retval) {
2322 dev_err(&pdev->dev, "Unable to reserve resources\n");
2323 goto err_resource;
2324 }
2325
2326 /* map registers in BAR 0 */
Martyn Welch29848ac2010-02-18 15:13:05 +00002327 tsi148_device->base = ioremap_nocache(pci_resource_start(pdev, 0),
2328 4096);
2329 if (!tsi148_device->base) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002330 dev_err(&pdev->dev, "Unable to remap CRG region\n");
2331 retval = -EIO;
2332 goto err_remap;
2333 }
2334
2335 /* Check to see if the mapping worked out */
Martyn Welch29848ac2010-02-18 15:13:05 +00002336 data = ioread32(tsi148_device->base + TSI148_PCFS_ID) & 0x0000FFFF;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002337 if (data != PCI_VENDOR_ID_TUNDRA) {
2338 dev_err(&pdev->dev, "CRG region check failed\n");
2339 retval = -EIO;
2340 goto err_test;
2341 }
2342
2343 /* Initialize wait queues & mutual exclusion flags */
Emilio G. Cota886953e2010-11-12 11:14:07 +00002344 init_waitqueue_head(&tsi148_device->dma_queue[0]);
2345 init_waitqueue_head(&tsi148_device->dma_queue[1]);
2346 init_waitqueue_head(&tsi148_device->iack_queue);
2347 mutex_init(&tsi148_device->vme_int);
2348 mutex_init(&tsi148_device->vme_rmw);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002349
Emilio G. Cota886953e2010-11-12 11:14:07 +00002350 tsi148_bridge->parent = &pdev->dev;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002351 strcpy(tsi148_bridge->name, driver_name);
2352
2353 /* Setup IRQ */
2354 retval = tsi148_irq_init(tsi148_bridge);
2355 if (retval != 0) {
2356 dev_err(&pdev->dev, "Chip Initialization failed.\n");
2357 goto err_irq;
2358 }
2359
2360 /* If we are going to flush writes, we need to read from the VME bus.
2361 * We need to do this safely, thus we read the devices own CR/CSR
2362 * register. To do this we must set up a window in CR/CSR space and
2363 * hence have one less master window resource available.
2364 */
2365 master_num = TSI148_MAX_MASTER;
Martyn Welch79463282010-03-22 14:58:57 +00002366 if (err_chk) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002367 master_num--;
Martyn Welch29848ac2010-02-18 15:13:05 +00002368
Julia Lawall32414872010-05-11 20:26:57 +02002369 tsi148_device->flush_image =
Martyn Welch29848ac2010-02-18 15:13:05 +00002370 kmalloc(sizeof(struct vme_master_resource), GFP_KERNEL);
2371 if (tsi148_device->flush_image == NULL) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002372 dev_err(&pdev->dev, "Failed to allocate memory for "
2373 "flush resource structure\n");
2374 retval = -ENOMEM;
2375 goto err_master;
2376 }
Martyn Welch29848ac2010-02-18 15:13:05 +00002377 tsi148_device->flush_image->parent = tsi148_bridge;
Emilio G. Cota886953e2010-11-12 11:14:07 +00002378 spin_lock_init(&tsi148_device->flush_image->lock);
Martyn Welch29848ac2010-02-18 15:13:05 +00002379 tsi148_device->flush_image->locked = 1;
2380 tsi148_device->flush_image->number = master_num;
Emilio G. Cota886953e2010-11-12 11:14:07 +00002381 memset(&tsi148_device->flush_image->bus_resource, 0,
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002382 sizeof(struct resource));
Martyn Welch29848ac2010-02-18 15:13:05 +00002383 tsi148_device->flush_image->kern_base = NULL;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002384 }
2385
2386 /* Add master windows to list */
Emilio G. Cota886953e2010-11-12 11:14:07 +00002387 INIT_LIST_HEAD(&tsi148_bridge->master_resources);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002388 for (i = 0; i < master_num; i++) {
Martyn Welch79463282010-03-22 14:58:57 +00002389 master_image = kmalloc(sizeof(struct vme_master_resource),
2390 GFP_KERNEL);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002391 if (master_image == NULL) {
2392 dev_err(&pdev->dev, "Failed to allocate memory for "
2393 "master resource structure\n");
2394 retval = -ENOMEM;
2395 goto err_master;
2396 }
2397 master_image->parent = tsi148_bridge;
Emilio G. Cota886953e2010-11-12 11:14:07 +00002398 spin_lock_init(&master_image->lock);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002399 master_image->locked = 0;
2400 master_image->number = i;
2401 master_image->address_attr = VME_A16 | VME_A24 | VME_A32 |
Martyn Welch08e03c22015-02-26 18:53:11 +03002402 VME_A64 | VME_CRCSR | VME_USER1 | VME_USER2 |
2403 VME_USER3 | VME_USER4;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002404 master_image->cycle_attr = VME_SCT | VME_BLT | VME_MBLT |
2405 VME_2eVME | VME_2eSST | VME_2eSSTB | VME_2eSST160 |
2406 VME_2eSST267 | VME_2eSST320 | VME_SUPER | VME_USER |
2407 VME_PROG | VME_DATA;
2408 master_image->width_attr = VME_D16 | VME_D32;
Emilio G. Cota886953e2010-11-12 11:14:07 +00002409 memset(&master_image->bus_resource, 0,
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002410 sizeof(struct resource));
2411 master_image->kern_base = NULL;
Emilio G. Cota886953e2010-11-12 11:14:07 +00002412 list_add_tail(&master_image->list,
2413 &tsi148_bridge->master_resources);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002414 }
2415
2416 /* Add slave windows to list */
Emilio G. Cota886953e2010-11-12 11:14:07 +00002417 INIT_LIST_HEAD(&tsi148_bridge->slave_resources);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002418 for (i = 0; i < TSI148_MAX_SLAVE; i++) {
Martyn Welch79463282010-03-22 14:58:57 +00002419 slave_image = kmalloc(sizeof(struct vme_slave_resource),
2420 GFP_KERNEL);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002421 if (slave_image == NULL) {
2422 dev_err(&pdev->dev, "Failed to allocate memory for "
2423 "slave resource structure\n");
2424 retval = -ENOMEM;
2425 goto err_slave;
2426 }
2427 slave_image->parent = tsi148_bridge;
Emilio G. Cota886953e2010-11-12 11:14:07 +00002428 mutex_init(&slave_image->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002429 slave_image->locked = 0;
2430 slave_image->number = i;
2431 slave_image->address_attr = VME_A16 | VME_A24 | VME_A32 |
Martyn Welch08e03c22015-02-26 18:53:11 +03002432 VME_A64;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002433 slave_image->cycle_attr = VME_SCT | VME_BLT | VME_MBLT |
2434 VME_2eVME | VME_2eSST | VME_2eSSTB | VME_2eSST160 |
2435 VME_2eSST267 | VME_2eSST320 | VME_SUPER | VME_USER |
2436 VME_PROG | VME_DATA;
Emilio G. Cota886953e2010-11-12 11:14:07 +00002437 list_add_tail(&slave_image->list,
2438 &tsi148_bridge->slave_resources);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002439 }
2440
2441 /* Add dma engines to list */
Emilio G. Cota886953e2010-11-12 11:14:07 +00002442 INIT_LIST_HEAD(&tsi148_bridge->dma_resources);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002443 for (i = 0; i < TSI148_MAX_DMA; i++) {
Martyn Welch79463282010-03-22 14:58:57 +00002444 dma_ctrlr = kmalloc(sizeof(struct vme_dma_resource),
2445 GFP_KERNEL);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002446 if (dma_ctrlr == NULL) {
2447 dev_err(&pdev->dev, "Failed to allocate memory for "
2448 "dma resource structure\n");
2449 retval = -ENOMEM;
2450 goto err_dma;
2451 }
2452 dma_ctrlr->parent = tsi148_bridge;
Emilio G. Cota886953e2010-11-12 11:14:07 +00002453 mutex_init(&dma_ctrlr->mtx);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002454 dma_ctrlr->locked = 0;
2455 dma_ctrlr->number = i;
Martyn Welch4f723df2010-02-18 15:12:58 +00002456 dma_ctrlr->route_attr = VME_DMA_VME_TO_MEM |
2457 VME_DMA_MEM_TO_VME | VME_DMA_VME_TO_VME |
2458 VME_DMA_MEM_TO_MEM | VME_DMA_PATTERN_TO_VME |
2459 VME_DMA_PATTERN_TO_MEM;
Emilio G. Cota886953e2010-11-12 11:14:07 +00002460 INIT_LIST_HEAD(&dma_ctrlr->pending);
2461 INIT_LIST_HEAD(&dma_ctrlr->running);
2462 list_add_tail(&dma_ctrlr->list,
2463 &tsi148_bridge->dma_resources);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002464 }
2465
Martyn Welch42fb5032009-08-11 17:44:56 +01002466 /* Add location monitor to list */
Emilio G. Cota886953e2010-11-12 11:14:07 +00002467 INIT_LIST_HEAD(&tsi148_bridge->lm_resources);
Martyn Welch42fb5032009-08-11 17:44:56 +01002468 lm = kmalloc(sizeof(struct vme_lm_resource), GFP_KERNEL);
2469 if (lm == NULL) {
2470 dev_err(&pdev->dev, "Failed to allocate memory for "
2471 "location monitor resource structure\n");
2472 retval = -ENOMEM;
2473 goto err_lm;
2474 }
2475 lm->parent = tsi148_bridge;
Emilio G. Cota886953e2010-11-12 11:14:07 +00002476 mutex_init(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01002477 lm->locked = 0;
2478 lm->number = 1;
2479 lm->monitors = 4;
Emilio G. Cota886953e2010-11-12 11:14:07 +00002480 list_add_tail(&lm->list, &tsi148_bridge->lm_resources);
Martyn Welch42fb5032009-08-11 17:44:56 +01002481
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002482 tsi148_bridge->slave_get = tsi148_slave_get;
2483 tsi148_bridge->slave_set = tsi148_slave_set;
2484 tsi148_bridge->master_get = tsi148_master_get;
2485 tsi148_bridge->master_set = tsi148_master_set;
2486 tsi148_bridge->master_read = tsi148_master_read;
2487 tsi148_bridge->master_write = tsi148_master_write;
2488 tsi148_bridge->master_rmw = tsi148_master_rmw;
2489 tsi148_bridge->dma_list_add = tsi148_dma_list_add;
2490 tsi148_bridge->dma_list_exec = tsi148_dma_list_exec;
2491 tsi148_bridge->dma_list_empty = tsi148_dma_list_empty;
Martyn Welchc813f592009-10-29 16:34:54 +00002492 tsi148_bridge->irq_set = tsi148_irq_set;
2493 tsi148_bridge->irq_generate = tsi148_irq_generate;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002494 tsi148_bridge->lm_set = tsi148_lm_set;
2495 tsi148_bridge->lm_get = tsi148_lm_get;
2496 tsi148_bridge->lm_attach = tsi148_lm_attach;
2497 tsi148_bridge->lm_detach = tsi148_lm_detach;
2498 tsi148_bridge->slot_get = tsi148_slot_get;
Manohar Vanga7f58f022011-08-10 11:33:46 +02002499 tsi148_bridge->alloc_consistent = tsi148_alloc_consistent;
2500 tsi148_bridge->free_consistent = tsi148_free_consistent;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002501
Martyn Welch29848ac2010-02-18 15:13:05 +00002502 data = ioread32be(tsi148_device->base + TSI148_LCSR_VSTAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002503 dev_info(&pdev->dev, "Board is%s the VME system controller\n",
Martyn Welch79463282010-03-22 14:58:57 +00002504 (data & TSI148_LCSR_VSTAT_SCONS) ? "" : " not");
Martyn Welch29848ac2010-02-18 15:13:05 +00002505 if (!geoid)
Martyn Welch638f1992009-12-15 08:42:49 +00002506 dev_info(&pdev->dev, "VME geographical address is %d\n",
2507 data & TSI148_LCSR_VSTAT_GA_M);
Martyn Welch29848ac2010-02-18 15:13:05 +00002508 else
Martyn Welch638f1992009-12-15 08:42:49 +00002509 dev_info(&pdev->dev, "VME geographical address is set to %d\n",
2510 geoid);
Martyn Welch29848ac2010-02-18 15:13:05 +00002511
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002512 dev_info(&pdev->dev, "VME Write and flush and error check is %s\n",
2513 err_chk ? "enabled" : "disabled");
2514
Wei Yongjun0686ab72013-06-19 10:42:35 +08002515 retval = tsi148_crcsr_init(tsi148_bridge, pdev);
2516 if (retval) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002517 dev_err(&pdev->dev, "CR/CSR configuration failed.\n");
2518 goto err_crcsr;
Martyn Welch48397372010-03-22 14:58:43 +00002519 }
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002520
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002521 retval = vme_register_bridge(tsi148_bridge);
2522 if (retval != 0) {
2523 dev_err(&pdev->dev, "Chip Registration failed.\n");
2524 goto err_reg;
2525 }
2526
Martyn Welch29848ac2010-02-18 15:13:05 +00002527 pci_set_drvdata(pdev, tsi148_bridge);
2528
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002529 /* Clear VME bus "board fail", and "power-up reset" lines */
Martyn Welch29848ac2010-02-18 15:13:05 +00002530 data = ioread32be(tsi148_device->base + TSI148_LCSR_VSTAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002531 data &= ~TSI148_LCSR_VSTAT_BRDFL;
2532 data |= TSI148_LCSR_VSTAT_CPURST;
Martyn Welch29848ac2010-02-18 15:13:05 +00002533 iowrite32be(data, tsi148_device->base + TSI148_LCSR_VSTAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002534
2535 return 0;
2536
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002537err_reg:
Martyn Welch29848ac2010-02-18 15:13:05 +00002538 tsi148_crcsr_exit(tsi148_bridge, pdev);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002539err_crcsr:
Martyn Welch42fb5032009-08-11 17:44:56 +01002540err_lm:
2541 /* resources are stored in link list */
Wei Yongjunb49c32b2012-08-21 12:19:01 +08002542 list_for_each_safe(pos, n, &tsi148_bridge->lm_resources) {
Martyn Welch42fb5032009-08-11 17:44:56 +01002543 lm = list_entry(pos, struct vme_lm_resource, list);
2544 list_del(pos);
2545 kfree(lm);
2546 }
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002547err_dma:
2548 /* resources are stored in link list */
Wei Yongjunb49c32b2012-08-21 12:19:01 +08002549 list_for_each_safe(pos, n, &tsi148_bridge->dma_resources) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002550 dma_ctrlr = list_entry(pos, struct vme_dma_resource, list);
2551 list_del(pos);
2552 kfree(dma_ctrlr);
2553 }
2554err_slave:
2555 /* resources are stored in link list */
Wei Yongjunb49c32b2012-08-21 12:19:01 +08002556 list_for_each_safe(pos, n, &tsi148_bridge->slave_resources) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002557 slave_image = list_entry(pos, struct vme_slave_resource, list);
2558 list_del(pos);
2559 kfree(slave_image);
2560 }
2561err_master:
2562 /* resources are stored in link list */
Wei Yongjunb49c32b2012-08-21 12:19:01 +08002563 list_for_each_safe(pos, n, &tsi148_bridge->master_resources) {
Martyn Welch79463282010-03-22 14:58:57 +00002564 master_image = list_entry(pos, struct vme_master_resource,
2565 list);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002566 list_del(pos);
2567 kfree(master_image);
2568 }
2569
Emilio G. Cotaa82ad052010-11-12 11:14:47 +00002570 tsi148_irq_exit(tsi148_bridge, pdev);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002571err_irq:
2572err_test:
Martyn Welch29848ac2010-02-18 15:13:05 +00002573 iounmap(tsi148_device->base);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002574err_remap:
2575 pci_release_regions(pdev);
2576err_resource:
2577 pci_disable_device(pdev);
2578err_enable:
Martyn Welch29848ac2010-02-18 15:13:05 +00002579 kfree(tsi148_device);
2580err_driver:
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002581 kfree(tsi148_bridge);
2582err_struct:
2583 return retval;
2584
2585}
2586
2587static void tsi148_remove(struct pci_dev *pdev)
2588{
2589 struct list_head *pos = NULL;
Emilio G. Cotab558ba22010-11-12 11:14:34 +00002590 struct list_head *tmplist;
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002591 struct vme_master_resource *master_image;
2592 struct vme_slave_resource *slave_image;
2593 struct vme_dma_resource *dma_ctrlr;
2594 int i;
Martyn Welch29848ac2010-02-18 15:13:05 +00002595 struct tsi148_driver *bridge;
2596 struct vme_bridge *tsi148_bridge = pci_get_drvdata(pdev);
2597
2598 bridge = tsi148_bridge->driver_priv;
2599
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002600
2601 dev_dbg(&pdev->dev, "Driver is being unloaded.\n");
2602
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002603 /*
2604 * Shutdown all inbound and outbound windows.
2605 */
2606 for (i = 0; i < 8; i++) {
Martyn Welch29848ac2010-02-18 15:13:05 +00002607 iowrite32be(0, bridge->base + TSI148_LCSR_IT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002608 TSI148_LCSR_OFFSET_ITAT);
Martyn Welch29848ac2010-02-18 15:13:05 +00002609 iowrite32be(0, bridge->base + TSI148_LCSR_OT[i] +
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002610 TSI148_LCSR_OFFSET_OTAT);
2611 }
2612
2613 /*
2614 * Shutdown Location monitor.
2615 */
Martyn Welch29848ac2010-02-18 15:13:05 +00002616 iowrite32be(0, bridge->base + TSI148_LCSR_LMAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002617
2618 /*
2619 * Shutdown CRG map.
2620 */
Martyn Welch29848ac2010-02-18 15:13:05 +00002621 iowrite32be(0, bridge->base + TSI148_LCSR_CSRAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002622
2623 /*
2624 * Clear error status.
2625 */
Martyn Welch29848ac2010-02-18 15:13:05 +00002626 iowrite32be(0xFFFFFFFF, bridge->base + TSI148_LCSR_EDPAT);
2627 iowrite32be(0xFFFFFFFF, bridge->base + TSI148_LCSR_VEAT);
2628 iowrite32be(0x07000700, bridge->base + TSI148_LCSR_PSTAT);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002629
2630 /*
2631 * Remove VIRQ interrupt (if any)
2632 */
Martyn Welch29848ac2010-02-18 15:13:05 +00002633 if (ioread32be(bridge->base + TSI148_LCSR_VICR) & 0x800)
2634 iowrite32be(0x8000, bridge->base + TSI148_LCSR_VICR);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002635
2636 /*
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002637 * Map all Interrupts to PCI INTA
2638 */
Martyn Welch29848ac2010-02-18 15:13:05 +00002639 iowrite32be(0x0, bridge->base + TSI148_LCSR_INTM1);
2640 iowrite32be(0x0, bridge->base + TSI148_LCSR_INTM2);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002641
Emilio G. Cotaa82ad052010-11-12 11:14:47 +00002642 tsi148_irq_exit(tsi148_bridge, pdev);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002643
2644 vme_unregister_bridge(tsi148_bridge);
2645
Martyn Welch29848ac2010-02-18 15:13:05 +00002646 tsi148_crcsr_exit(tsi148_bridge, pdev);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002647
2648 /* resources are stored in link list */
Emilio G. Cotab558ba22010-11-12 11:14:34 +00002649 list_for_each_safe(pos, tmplist, &tsi148_bridge->dma_resources) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002650 dma_ctrlr = list_entry(pos, struct vme_dma_resource, list);
2651 list_del(pos);
2652 kfree(dma_ctrlr);
2653 }
2654
2655 /* resources are stored in link list */
Emilio G. Cotab558ba22010-11-12 11:14:34 +00002656 list_for_each_safe(pos, tmplist, &tsi148_bridge->slave_resources) {
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002657 slave_image = list_entry(pos, struct vme_slave_resource, list);
2658 list_del(pos);
2659 kfree(slave_image);
2660 }
2661
2662 /* resources are stored in link list */
Emilio G. Cotab558ba22010-11-12 11:14:34 +00002663 list_for_each_safe(pos, tmplist, &tsi148_bridge->master_resources) {
Martyn Welch638f1992009-12-15 08:42:49 +00002664 master_image = list_entry(pos, struct vme_master_resource,
2665 list);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002666 list_del(pos);
2667 kfree(master_image);
2668 }
2669
Martyn Welch29848ac2010-02-18 15:13:05 +00002670 iounmap(bridge->base);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002671
2672 pci_release_regions(pdev);
2673
2674 pci_disable_device(pdev);
2675
Martyn Welch29848ac2010-02-18 15:13:05 +00002676 kfree(tsi148_bridge->driver_priv);
2677
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002678 kfree(tsi148_bridge);
2679}
2680
Wei Yongjun01c07142012-10-18 23:12:50 +08002681module_pci_driver(tsi148_driver);
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002682
2683MODULE_PARM_DESC(err_chk, "Check for VME errors on reads and writes");
2684module_param(err_chk, bool, 0);
2685
Martyn Welch638f1992009-12-15 08:42:49 +00002686MODULE_PARM_DESC(geoid, "Override geographical addressing");
2687module_param(geoid, int, 0);
2688
Martyn Welchd22b8ed2009-07-31 09:28:17 +01002689MODULE_DESCRIPTION("VME driver for the Tundra Tempe VME bridge");
2690MODULE_LICENSE("GPL");