blob: b6234203e0ac19404ed2943d5e0c808953907dc6 [file] [log] [blame]
Paul Mundt959f85f2006-09-27 16:43:28 +09001/*
2 * Generic SH-4 / SH-4A PCIC operations (SH7751, SH7780).
3 *
Paul Mundt7e4ba0d2009-04-17 14:07:57 +09004 * Copyright (C) 2002 - 2009 Paul Mundt
Paul Mundt959f85f2006-09-27 16:43:28 +09005 *
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License v2. See the file "COPYING" in the main directory of this archive
8 * for more details.
9 */
10#include <linux/pci.h>
Paul Mundt7e4ba0d2009-04-17 14:07:57 +090011#include <linux/io.h>
Paul Mundt39a90862010-09-20 18:56:13 +090012#include <linux/spinlock.h>
Paul Mundt959f85f2006-09-27 16:43:28 +090013#include <asm/addrspace.h>
Paul Mundt959f85f2006-09-27 16:43:28 +090014#include "pci-sh4.h"
15
16/*
17 * Direct access to PCI hardware...
18 */
19#define CONFIG_CMD(bus, devfn, where) \
Paul Mundtef407be2010-02-01 16:39:46 +090020 (0x80000000 | (bus->number << 16) | (devfn << 8) | (where & ~3))
Paul Mundt959f85f2006-09-27 16:43:28 +090021
Paul Mundt959f85f2006-09-27 16:43:28 +090022/*
23 * Functions for accessing PCI configuration space with type 1 accesses
24 */
25static int sh4_pci_read(struct pci_bus *bus, unsigned int devfn,
26 int where, int size, u32 *val)
27{
Magnus Dammb6706ef2008-02-19 21:34:55 +090028 struct pci_channel *chan = bus->sysdata;
Paul Mundt959f85f2006-09-27 16:43:28 +090029 unsigned long flags;
30 u32 data;
31
32 /*
33 * PCIPDR may only be accessed as 32 bit words,
34 * so we must do byte alignment by hand
35 */
Paul Mundt39a90862010-09-20 18:56:13 +090036 raw_spin_lock_irqsave(&pci_config_lock, flags);
Magnus Dammb6706ef2008-02-19 21:34:55 +090037 pci_write_reg(chan, CONFIG_CMD(bus, devfn, where), SH4_PCIPAR);
38 data = pci_read_reg(chan, SH4_PCIPDR);
Paul Mundt39a90862010-09-20 18:56:13 +090039 raw_spin_unlock_irqrestore(&pci_config_lock, flags);
Paul Mundt959f85f2006-09-27 16:43:28 +090040
41 switch (size) {
42 case 1:
43 *val = (data >> ((where & 3) << 3)) & 0xff;
44 break;
45 case 2:
46 *val = (data >> ((where & 2) << 3)) & 0xffff;
47 break;
48 case 4:
49 *val = data;
50 break;
51 default:
52 return PCIBIOS_FUNC_NOT_SUPPORTED;
53 }
54
55 return PCIBIOS_SUCCESSFUL;
56}
57
58/*
59 * Since SH4 only does 32bit access we'll have to do a read,
60 * mask,write operation.
61 * We'll allow an odd byte offset, though it should be illegal.
62 */
63static int sh4_pci_write(struct pci_bus *bus, unsigned int devfn,
64 int where, int size, u32 val)
65{
Magnus Dammb6706ef2008-02-19 21:34:55 +090066 struct pci_channel *chan = bus->sysdata;
Paul Mundt959f85f2006-09-27 16:43:28 +090067 unsigned long flags;
68 int shift;
69 u32 data;
70
Paul Mundt39a90862010-09-20 18:56:13 +090071 raw_spin_lock_irqsave(&pci_config_lock, flags);
Magnus Dammb6706ef2008-02-19 21:34:55 +090072 pci_write_reg(chan, CONFIG_CMD(bus, devfn, where), SH4_PCIPAR);
73 data = pci_read_reg(chan, SH4_PCIPDR);
Paul Mundt39a90862010-09-20 18:56:13 +090074 raw_spin_unlock_irqrestore(&pci_config_lock, flags);
Paul Mundt959f85f2006-09-27 16:43:28 +090075
76 switch (size) {
77 case 1:
78 shift = (where & 3) << 3;
79 data &= ~(0xff << shift);
80 data |= ((val & 0xff) << shift);
81 break;
82 case 2:
83 shift = (where & 2) << 3;
84 data &= ~(0xffff << shift);
85 data |= ((val & 0xffff) << shift);
86 break;
87 case 4:
88 data = val;
89 break;
90 default:
91 return PCIBIOS_FUNC_NOT_SUPPORTED;
92 }
93
Magnus Dammb6706ef2008-02-19 21:34:55 +090094 pci_write_reg(chan, data, SH4_PCIPDR);
Paul Mundt959f85f2006-09-27 16:43:28 +090095
96 return PCIBIOS_SUCCESSFUL;
97}
98
99struct pci_ops sh4_pci_ops = {
100 .read = sh4_pci_read,
101 .write = sh4_pci_write,
102};
103
Magnus Dammb8b47bf2009-03-11 15:41:51 +0900104int __attribute__((weak)) pci_fixup_pcic(struct pci_channel *chan)
Paul Mundtcd6c7ea2007-03-29 00:04:39 +0900105{
106 /* Nothing to do. */
107 return 0;
108}