blob: 9c5dff06e6b998b95cbfda37a273c28739e768e8 [file] [log] [blame]
Stefan Reinauereca92fb2006-08-23 14:28:37 +00001/*
Stefan Reinauer7e61e452008-01-18 10:35:56 +00002 * This file is part of the coreboot project.
Stefan Reinauereca92fb2006-08-23 14:28:37 +00003 *
Uwe Hermannd82baa12006-12-05 14:13:10 +00004 * Copyright (C) 2000 AG Electronics Ltd.
5 * Copyright (C) 2003-2004 Linux Networx
Zheng Bao9db833b2009-12-28 09:59:44 +00006 * Copyright (C) 2004 Tyan
Stefan Reinauereca92fb2006-08-23 14:28:37 +00007 * Copyright (C) 2005 Digital Design Corporation
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
Stefan Reinauereca92fb2006-08-23 14:28:37 +000018 */
19
Uwe Hermannd82baa12006-12-05 14:13:10 +000020/* RAM-based driver for SMSC LPC47N217 Super I/O chip. */
Uwe Hermannd82baa12006-12-05 14:13:10 +000021
Stefan Reinauereca92fb2006-08-23 14:28:37 +000022#include <arch/io.h>
23#include <device/device.h>
24#include <device/pnp.h>
25#include <console/console.h>
26#include <device/smbus.h>
27#include <string.h>
Stefan Reinauereca92fb2006-08-23 14:28:37 +000028#include <assert.h>
Carl-Daniel Hailfinger2ee67792008-10-01 12:52:52 +000029#include <stdlib.h>
Stefan Reinauereca92fb2006-08-23 14:28:37 +000030#include "lpc47n217.h"
31
Zheng Bao9db833b2009-12-28 09:59:44 +000032/* Forward declarations */
Edward O'Callaghanf21bdc32014-10-21 07:43:41 +110033static void enable_dev(struct device *dev);
34static void lpc47n217_pnp_set_resources(struct device *dev);
35static void lpc47n217_pnp_enable_resources(struct device *dev);
36static void lpc47n217_pnp_enable(struct device *dev);
37static void lpc47n217_init(struct device *dev);
38static void lpc47n217_pnp_set_resource(struct device *dev, struct resource *resource);
39static void lpc47n217_pnp_set_iobase(struct device *dev, u16 iobase);
40static void lpc47n217_pnp_set_drq(struct device *dev, u8 drq);
41static void lpc47n217_pnp_set_irq(struct device *dev, u8 irq);
42static void lpc47n217_pnp_set_enable(struct device *dev, int enable);
43static void pnp_enter_conf_state(struct device *dev);
44static void pnp_exit_conf_state(struct device *dev);
Stefan Reinauereca92fb2006-08-23 14:28:37 +000045
Stefan Reinauereca92fb2006-08-23 14:28:37 +000046struct chip_operations superio_smsc_lpc47n217_ops = {
Uwe Hermanna7aa29b2006-11-05 18:50:49 +000047 CHIP_NAME("SMSC LPC47N217 Super I/O")
Stefan Reinauereca92fb2006-08-23 14:28:37 +000048 .enable_dev = enable_dev,
49};
50
51static struct device_operations ops = {
52 .read_resources = pnp_read_resources,
53 .set_resources = lpc47n217_pnp_set_resources,
54 .enable_resources = lpc47n217_pnp_enable_resources,
55 .enable = lpc47n217_pnp_enable,
56 .init = lpc47n217_init,
57};
58
59static struct pnp_info pnp_dev_info[] = {
Samuel Holland7daac912017-06-06 22:55:01 -050060 { &ops, LPC47N217_PP, PNP_IO0 | PNP_IRQ0 | PNP_DRQ0, 0x07f8, },
61 { &ops, LPC47N217_SP1, PNP_IO0 | PNP_IRQ0, 0x07f8, },
62 { &ops, LPC47N217_SP2, PNP_IO0 | PNP_IRQ0, 0x07f8, }
Stefan Reinauereca92fb2006-08-23 14:28:37 +000063};
64
Uwe Hermannb69cb5a2010-10-26 22:46:43 +000065/**
66 * Create device structures and allocate resources to devices specified in the
67 * pnp_dev_info array (above).
68 *
69 * @param dev Pointer to structure describing a Super I/O device.
Zheng Bao9db833b2009-12-28 09:59:44 +000070 */
Edward O'Callaghanf21bdc32014-10-21 07:43:41 +110071static void enable_dev(struct device *dev)
Stefan Reinauereca92fb2006-08-23 14:28:37 +000072{
Uwe Hermannb69cb5a2010-10-26 22:46:43 +000073 pnp_enable_devices(dev, &pnp_ops, ARRAY_SIZE(pnp_dev_info),
Zheng Bao9db833b2009-12-28 09:59:44 +000074 pnp_dev_info);
Stefan Reinauereca92fb2006-08-23 14:28:37 +000075}
76
Uwe Hermannb69cb5a2010-10-26 22:46:43 +000077/**
78 * Configure the specified Super I/O device with the resources (I/O space,
79 * etc.) that have been allocate for it.
80 *
Uwe Hermanna69d9782010-11-15 19:35:14 +000081 * NOTE: Cannot use pnp_set_resources() here because it assumes chip
82 * support for logical devices, which the LPC47N217 doesn't have.
83 *
Uwe Hermannb69cb5a2010-10-26 22:46:43 +000084 * @param dev Pointer to structure describing a Super I/O device.
Zheng Bao9db833b2009-12-28 09:59:44 +000085 */
Edward O'Callaghanf21bdc32014-10-21 07:43:41 +110086static void lpc47n217_pnp_set_resources(struct device *dev)
Stefan Reinauereca92fb2006-08-23 14:28:37 +000087{
Myles Watsonc25cc112010-05-21 14:33:48 +000088 struct resource *res;
Stefan Reinauereca92fb2006-08-23 14:28:37 +000089
Zheng Bao9db833b2009-12-28 09:59:44 +000090 pnp_enter_conf_state(dev);
Uwe Hermanna69d9782010-11-15 19:35:14 +000091 for (res = dev->resource_list; res; res = res->next)
Myles Watsonc25cc112010-05-21 14:33:48 +000092 lpc47n217_pnp_set_resource(dev, res);
Zheng Bao9db833b2009-12-28 09:59:44 +000093 /* dump_pnp_device(dev); */
Zheng Bao9db833b2009-12-28 09:59:44 +000094 pnp_exit_conf_state(dev);
95}
Stefan Reinauereca92fb2006-08-23 14:28:37 +000096
Uwe Hermanna69d9782010-11-15 19:35:14 +000097/*
98 * NOTE: Cannot use pnp_enable_resources() here because it assumes chip
99 * support for logical devices, which the LPC47N217 doesn't have.
100 */
Edward O'Callaghanf21bdc32014-10-21 07:43:41 +1100101static void lpc47n217_pnp_enable_resources(struct device *dev)
Zheng Bao9db833b2009-12-28 09:59:44 +0000102{
103 pnp_enter_conf_state(dev);
Zheng Bao9db833b2009-12-28 09:59:44 +0000104 lpc47n217_pnp_set_enable(dev, 1);
Zheng Bao9db833b2009-12-28 09:59:44 +0000105 pnp_exit_conf_state(dev);
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000106}
107
Uwe Hermanna69d9782010-11-15 19:35:14 +0000108/*
109 * NOTE: Cannot use pnp_set_enable() here because it assumes chip
110 * support for logical devices, which the LPC47N217 doesn't have.
111 */
Edward O'Callaghanf21bdc32014-10-21 07:43:41 +1100112static void lpc47n217_pnp_enable(struct device *dev)
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000113{
Zheng Bao9db833b2009-12-28 09:59:44 +0000114 pnp_enter_conf_state(dev);
Rudolf Marek0c8e6642011-02-19 14:51:31 +0000115 lpc47n217_pnp_set_enable(dev, !!dev->enabled);
Zheng Bao9db833b2009-12-28 09:59:44 +0000116 pnp_exit_conf_state(dev);
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000117}
118
Uwe Hermannb69cb5a2010-10-26 22:46:43 +0000119/**
120 * Initialize the specified Super I/O device.
121 *
122 * Devices other than COM ports are ignored. For COM ports, we configure the
123 * baud rate.
124 *
125 * @param dev Pointer to structure describing a Super I/O device.
Zheng Bao9db833b2009-12-28 09:59:44 +0000126 */
Edward O'Callaghanf21bdc32014-10-21 07:43:41 +1100127static void lpc47n217_init(struct device *dev)
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000128{
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000129 if (!dev->enabled)
130 return;
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000131}
132
Edward O'Callaghanf21bdc32014-10-21 07:43:41 +1100133static void lpc47n217_pnp_set_resource(struct device *dev, struct resource *resource)
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000134{
135 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
zbao9bf356f2012-08-03 15:09:09 +0800136 printk(BIOS_ERR, "ERROR: %s %02lx not allocated\n",
Uwe Hermanna69d9782010-11-15 19:35:14 +0000137 dev_path(dev), resource->index);
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000138 return;
139 }
140
Uwe Hermanna69d9782010-11-15 19:35:14 +0000141 /* Now store the resource. */
142
Uwe Hermannb69cb5a2010-10-26 22:46:43 +0000143 /*
144 * NOTE: Cannot use pnp_set_XXX() here because they assume chip
145 * support for logical devices, which the LPC47N217 doesn't have.
Zheng Bao9db833b2009-12-28 09:59:44 +0000146 */
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000147 if (resource->flags & IORESOURCE_IO) {
148 lpc47n217_pnp_set_iobase(dev, resource->base);
Uwe Hermanna69d9782010-11-15 19:35:14 +0000149 } else if (resource->flags & IORESOURCE_DRQ) {
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000150 lpc47n217_pnp_set_drq(dev, resource->base);
Uwe Hermanna69d9782010-11-15 19:35:14 +0000151 } else if (resource->flags & IORESOURCE_IRQ) {
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000152 lpc47n217_pnp_set_irq(dev, resource->base);
Uwe Hermanna69d9782010-11-15 19:35:14 +0000153 } else {
zbao9bf356f2012-08-03 15:09:09 +0800154 printk(BIOS_ERR, "ERROR: %s %02lx unknown resource type\n",
Uwe Hermanna69d9782010-11-15 19:35:14 +0000155 dev_path(dev), resource->index);
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000156 return;
157 }
158 resource->flags |= IORESOURCE_STORED;
159
160 report_resource_stored(dev, resource, "");
161}
162
Edward O'Callaghanf21bdc32014-10-21 07:43:41 +1100163static void lpc47n217_pnp_set_iobase(struct device *dev, u16 iobase)
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000164{
165 ASSERT(!(iobase & 0x3));
Zheng Bao9db833b2009-12-28 09:59:44 +0000166
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000167 switch(dev->path.pnp.device) {
Zheng Bao9db833b2009-12-28 09:59:44 +0000168 case LPC47N217_PP:
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000169 pnp_write_config(dev, 0x23, (iobase >> 2) & 0xff);
170 break;
Zheng Bao9db833b2009-12-28 09:59:44 +0000171 case LPC47N217_SP1:
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000172 pnp_write_config(dev, 0x24, (iobase >> 2) & 0xff);
173 break;
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000174 case LPC47N217_SP2:
175 pnp_write_config(dev, 0x25, (iobase >> 2) & 0xff);
176 break;
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000177 default:
178 BUG();
179 break;
180 }
181}
182
Edward O'Callaghanf21bdc32014-10-21 07:43:41 +1100183static void lpc47n217_pnp_set_drq(struct device *dev, u8 drq)
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000184{
Uwe Hermanna69d9782010-11-15 19:35:14 +0000185 const u8 PP_DMA_MASK = 0x0F;
186 const u8 PP_DMA_SELECTION_REGISTER = 0x26;
187 u8 current_config, new_config;
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000188
Uwe Hermanna69d9782010-11-15 19:35:14 +0000189 if (dev->path.pnp.device == LPC47N217_PP) {
190 current_config = pnp_read_config(dev,
191 PP_DMA_SELECTION_REGISTER);
192 ASSERT(!(drq & ~PP_DMA_MASK)); /* DRQ out of range? */
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000193 new_config = (current_config & ~PP_DMA_MASK) | drq;
194 pnp_write_config(dev, PP_DMA_SELECTION_REGISTER, new_config);
195 } else {
196 BUG();
197 }
198}
199
Edward O'Callaghanf21bdc32014-10-21 07:43:41 +1100200static void lpc47n217_pnp_set_irq(struct device *dev, u8 irq)
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000201{
Uwe Hermanna69d9782010-11-15 19:35:14 +0000202 u8 irq_config_register = 0, irq_config_mask = 0;
203 u8 current_config, new_config;
Zheng Bao9db833b2009-12-28 09:59:44 +0000204
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000205 switch(dev->path.pnp.device) {
Zheng Bao9db833b2009-12-28 09:59:44 +0000206 case LPC47N217_PP:
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000207 irq_config_register = 0x27;
208 irq_config_mask = 0x0F;
209 break;
Zheng Bao9db833b2009-12-28 09:59:44 +0000210 case LPC47N217_SP1:
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000211 irq_config_register = 0x28;
212 irq_config_mask = 0xF0;
213 irq <<= 4;
214 break;
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000215 case LPC47N217_SP2:
216 irq_config_register = 0x28;
217 irq_config_mask = 0x0F;
218 break;
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000219 default:
220 BUG();
221 return;
222 }
223
Uwe Hermanna69d9782010-11-15 19:35:14 +0000224 ASSERT(!(irq & ~irq_config_mask)); /* IRQ out of range? */
Zheng Bao9db833b2009-12-28 09:59:44 +0000225
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000226 current_config = pnp_read_config(dev, irq_config_register);
227 new_config = (current_config & ~irq_config_mask) | irq;
228 pnp_write_config(dev, irq_config_register, new_config);
229}
230
Edward O'Callaghanf21bdc32014-10-21 07:43:41 +1100231static void lpc47n217_pnp_set_enable(struct device *dev, int enable)
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000232{
Uwe Hermanna69d9782010-11-15 19:35:14 +0000233 u8 power_register = 0, power_mask = 0, current_power, new_power;
Zheng Bao9db833b2009-12-28 09:59:44 +0000234
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000235 switch(dev->path.pnp.device) {
Zheng Bao9db833b2009-12-28 09:59:44 +0000236 case LPC47N217_PP:
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000237 power_register = 0x01;
238 power_mask = 0x04;
239 break;
Zheng Bao9db833b2009-12-28 09:59:44 +0000240 case LPC47N217_SP1:
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000241 power_register = 0x02;
242 power_mask = 0x08;
243 break;
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000244 case LPC47N217_SP2:
245 power_register = 0x02;
246 power_mask = 0x80;
247 break;
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000248 default:
249 BUG();
250 return;
251 }
252
253 current_power = pnp_read_config(dev, power_register);
Uwe Hermanna69d9782010-11-15 19:35:14 +0000254 new_power = current_power & ~power_mask; /* Disable by default. */
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000255 if (enable) {
Uwe Hermanna69d9782010-11-15 19:35:14 +0000256 struct resource* ioport_resource;
257 ioport_resource = find_resource(dev, PNP_IDX_IO0);
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000258 lpc47n217_pnp_set_iobase(dev, ioport_resource->base);
Uwe Hermanna69d9782010-11-15 19:35:14 +0000259 new_power |= power_mask; /* Enable. */
Zheng Bao9db833b2009-12-28 09:59:44 +0000260 } else {
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000261 lpc47n217_pnp_set_iobase(dev, 0);
262 }
263 pnp_write_config(dev, power_register, new_power);
264}
265
Edward O'Callaghanf21bdc32014-10-21 07:43:41 +1100266static void pnp_enter_conf_state(struct device *dev)
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000267{
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000268 outb(0x55, dev->path.pnp.port);
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000269}
270
Edward O'Callaghanf21bdc32014-10-21 07:43:41 +1100271static void pnp_exit_conf_state(struct device *dev)
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000272{
Zheng Bao9db833b2009-12-28 09:59:44 +0000273 outb(0xaa, dev->path.pnp.port);
Stefan Reinauereca92fb2006-08-23 14:28:37 +0000274}